Data Types in Go

Everything in Go language is a type. That being said, we should know that some data types are more complex than others. In this tutorial, we will learn the basic data types that are present in Go language.

In Go language, there are three main elementary types present. These are:

  • bool

  • numeric

  • string

Data Type Description
bool represents a boolean value
numeric represents a numeric value
string represents a collection of bytes

We will explore each of them with examples.

bool Datatype

The bool type represents a boolean value and the value of them is a predefined constant. It can either be true or false.

Syntax for bool:

var isTrue bool = false
isStudying := true

There are two ways we can create a bool variable in Go, both the approaches are mentioned in the syntax above. Let's make use of a bool variable in a Go example.

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var isSunday bool = true
	if isSunday {
		fmt.Println("Today is Sunday!")
	}
}

In the above program, we make use of a bool variable and use it later as a condition in the if condition, and if that evaluates to true we will print the message "Today is Sunday!" to the terminal.


Today is Sunday!

Numeric Datatype

Numeric types can in turn be split into different types. These are:

  • Integer and floating-point numbers

  • Complex numbers

  • character like type

Integer and floating-point numbers

In Go, some of the integer types have architecture-dependent types. These are:

  • int

  • uint

  • uintptr

All the above-mentioned data types are architecture-dependent. The architecture-dependent means that on a machine that is of 32-bit architecture, the int will take 32-bit size(4 bytes) and on a 64-bit architecture, the int will take the 64-bit size and the same goes for uint. Meanwhile, uintptr is an unsigned integer large enough to store a bit pattern of any pointer.

There are other types that are not architecture-dependent, these mainly are:

  • int8

  • int16

  • int32

  • int64

All the above are signed integers, which means they can contain both the -ve and +ve values in them.

The unsigned data types that are not architecture-dependent are:

  • uint8

  • uint16

  • uint32

  • uint64

Also, in Go language, floats are machine-independent, these are:

  • float32

  • float64

Note: It should be noted that float as its own type doesn't exist in Go.

Consider the below table which mentions the range of all the above-mentioned data types.

Data Types Range
int8

-128 to 127

int16

-32768 to 32767

int32

? 2,147,483,648 to 2,147,483,647

int64

? 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

uint8

0 to 255

uint16

0 to 65,535

uint32

0 to 4,294,967,295

uint64

0 to 18,446,744,073,709,551,615

Also, if we declare a variable with any of the above data types mentioned and do not provide a value to them then the compiler provides a default value to each of them. The default value for integer types is 0 and is the same for float32 and float64.

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var someValue int
	var anotherValue int32
	var someFloatValue float64
	fmt.Println("Default values are:", someValue, anotherValue, someFloatValue)
}


Default values are: 0 0 0

Go language is very strict when it comes to data types and their assignments to one another. It is possible to assign a value of one data type to another data type but only if they are of the same type, if we try to do that when they are not of the same data type the compiler will throw an error.

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var someValue int
	var anotherValue int32
	var someFloatValue float64
	anotherValue = someFloatValue
	fmt.Println("Done")
}

In the above example, we know that all the variables declared don't have any values assigned to them yet. So, all of them have the same default value i.e 0. Even though they contain the same value, we cannot assign one variable to another as the data type of them is not the same. The above code will result in an error.


./prog.go:11:15: cannot use someFloatValue (type float64) as type int32 in assignment

Now, let's write a program to make use of some of the integer and float data types mentioned above.

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var x int32 = 13
	y := 13.78
	var z uint = 11
	fmt.Println("Values are:", x, y, z)
}


Values are: 13 13.78 11

Interpreting the type

We can also extract the type of a variable with the help of the %T placeholder. The syntax for interpreting the type of a variable is:

fmt.Printf("%T",<variable name>)

Let's print the type of all the variables declared in the above-mentioned code.

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var x int32 = 13
	y := 13.78
	var z uint = 11
	fmt.Printf("Values are: %T, %T, %T", x, y, z)
}


Values are: int32, float64, uint

Complex numbers

A complex number is written in the form of:

re + im

re stands for the real part and im stands for the imaginary part.

The complex numbers in Go are of two types. These are:

  • complex64

  • complex128

Now, let's make use of a complex number in a Go program. Consider the example shown below:

package main

import "fmt"

func main() {
	// Declaring complex num (real +imaginary(ยก))
	var c1 complex64 = 6 + 10i
	fmt.Printf("The value is: %v", c1)
}


The value is: (6+10i)

c

Character like type

In this category, mainly rune falls. A rune is a type in Go language that is an alias for int32. It is used to store the Unicode point in Go. We will learn more about runes in the string tutorial later.

String Datatype

A string in Go language is a collection of bytes. A byte in Go is an alias for uint8. Each character in a string is basically a byte. The strings in Go follow Unicode formatting.

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	first := "Study"
	last := "Tonight"
	name := first + " " + last
	fmt.Println("My name is", name)
}

In the above example, we created two strings, namely first and last, and then used them later when we are printing them to the terminal.

It should be noted that we will learn more about strings, rune, and bytes in the upcoming string tutorial.

Conclusion

In the above article, we learned about the different data types that are present in Go, their uses, the size of each of the data types, and we also explored them with the help of several Go programs.