Basic Data Types in Go

The Go programming language is all about type. Everything has a type. Numbers, strings, arrays, slices, and even functions have a type that describes what the entity is and what it can do.

Variables are assigned a specific type when they’re created in Go. The type cannot be changed later like in Python and JavaScript. This restriction makes Go a statically-typed language.

To print a variable’s type, use the %T verb in the Printf() function of the fmt package. The Printf() function is a little different than the Println() function. It lets us pass a second argument into the verb, which in this case is %T, and return the variable type.

Run the example code below in your editor.

package main

import "fmt"

func main() {
	x := 42
	y := "text"
	fmt.Printf("x: %T\n", x)
	fmt.Printf("y: %T\n", y)
}

The output is:

x: int
y: string

In this post, we look at a few primitive data types in Go. These include integers, strings, and booleans.

Numeric Type

Go has two main number types: int and floating-point numbers. Int is short for integer, which are whole numbers. A floating-point number is a number with decimal places, like Pi (3.14159…).

Each of these numeric types have more specific variants. The Go specification for Numeric types lists eight different integer types and two types of floating-point numbers that can be created.

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

If you don’t specify the type when declaring numeric variables, whole numbers are automatically assigned the int type and decimal numbers are automatically assigned the float64 type. These default types are perfect for most practical purposes.

The code snippet below creates a num integer and assigns the value 40 to it. Then it creates a variable price and assigns 5.99 to it. The Go compiler recognizes 5.99 as a float value and creates price with a float64 type.

package main

import "fmt"

func main() {
	var num int = 40
	num += 1

	var price = 5.99

	fmt.Println(num)
	fmt.Println(price)
}

The output is:

41
5.99

String Type

In Go, a variable of type string is essentially a sequence of bytes that are used to represent alphanumeric characters.

To create a string, use double quotes or back ticks. Back ticks are used to create a raw, string literal. Raw strings include the extra tabs and line breaks that are used to create the string.

Run the following code below in your editor to see how the print output compares.

package main

import "fmt"

func main() {
	var lunch string = "ice cream sandwich"
	fmt.Println(lunch)

	lunch = `ice
	
	cream 
	
			sandwich`
	fmt.Println(lunch)
}

The output is:

ice cream sandwich
ice

		cream 

						sandwich

The most common methods for interacting with strings in Go are indexing, concatenation, and finding the length.

Access individual characters in strings by passing the index number into the square brackets [].

Run the following code snippet in your editor.

package main

import "fmt"

func main() {
	var flavor = "Peppermint"
	fmt.Println(flavor[0])
	fmt.Println(string(flavor[0]))
}

The output is:

80
P

When indexing the string flavor, the number 80 is returned. Why? Because 80 is the UTF-8 representation of the letter “P”. At their core, strings are basically just a series of bytes, which are integers. However, when you need to convert those integers back into alphanumeric representation, use the string() function.

Next, string concatenation is the process of combining two strings. This can be achieved with the + operator. An example of string concatenation is shown below.

var str1 = "Hello"
var str2 = ", "
var str3 = "World!"
fmt.Println(str1 + str2 + str3)

The output should be:

Hello, World!

Finally, use the len() function to get the length of the string in number of characters.

For example, the following print statement will output: 13

fmt.Println(len("Hello, World!"))

Keep in mind strings use 0-based indexing. The first character is at index 0, the second is 1, and so on.

Strings are immutable. You cannot change individual characters in a string once it’s created. However, you can reassign a new value to the variable.

Boolean Type

A boolean is a binary value and is denoted by the constants true and false. You often see booleans used in if-statements and iterative functions to test whether a conditions is true. And if a condition is true, a block of code is executed. We’ll explore if-statements and loops in later lessons.

The code example below declares a boolean x with a value of true.

package main

import "fmt"

func main() {
	var x bool = true
	fmt.Println(x)
}
true

Many comparative operations, expressions, and functions return boolean values.

The following code example prints false:

package main

import "fmt"

func main() {
	var x int = 3
	var y int = 2
	fmt.Println(x < y)
}
false

Shorthand Declarations

Go has a shorthand declaration operator := that can be used when creating local variables inside functions. This is meant to help narrow the scope of the variable and lets you type less code.

You do not need to use the keyword var or specify the variable type. Go will do all this for you.

Review the code snippet below.

package main

import "fmt"

func main() {
	flavor := "Vanilla"
	price := 5.99
	is_tasty := false
	fmt.Println(flavor)
	fmt.Println(price)
	fmt.Println(is_tasty)
}

The output is:

Vanilla
5.99
false

Summary

In this post we explored common primitive data types in Go: numbers, strings, and booleans. We also learned how to create variables using the shorthand decleration operator.

In the next post, we look at Arrays and Slices in Go.