5 minutes read

Go is a statically typed programming language. This means that variables are always of a certain type, and we can't change this type. At first glance, static typing may seem inconvenient. You will spend a lot of time just trying to fix error messages from the compiler that prevent the program from compiling. However, knowing the data types in advance allows us to understand why the program doesn't work properly even at the compilation stage and helps to avoid common mistakes. There are several built-in data types in Go, which we will now get acquainted with.

Integers

In Go, there are the following types of integers: uint8,uint16, uint32, uint64, int8, int16,int32, and int64. 8, 16, 32, and 64 tell us how many bits each type uses. uint means "unsigned integer", whilst int means "signed integer". An unsigned integer can only take positive values (or zero).

type

description

min value

max value

uint8

unsigned 8-bit integers

0

255

uint16

unsigned 16-bit integers

0

65535

uint32

unsigned 32-bit integers

0

2^32 - 1

uint64

unsigned 64-bit integers

0

2^64 - 1

uint

platform-dependent (32 or 64-bit)

0

platform-dependent

int8

signed 8-bit integers

-128

127

int16

signed 16-bit integers

-32768

32767

int32

signed 32-bit integers

-2 147 483 648

2 147 483 647

int64

signed 64-bit integers

-9 223 372 036 854 775 808

9 223 372 036 854 775 807

int

platform-dependent (32 or 64-bit)

platform-dependent

platform-dependent

There are different ways to declare a variable:

var number1 int
number1 = 100

var number2 int = 100

The type of the variable is determined at compile-time based on the value. All these variables get the int data type when compiled, despite the fact that we don't explicitly specify this:

var number1 = 100
number2 := 100

Floats

Floating-point numbers are numbers that contain a real part (real numbers), e.g. 1.234, 123.4, 0.00001234). The way they are represented in the computer is quite complicated and is not particularly necessary for their use. For now, we just have to remember the following:

Floating-point numbers can't represent every decimal fraction. There are cases when a number can't be represented at all. For example, the result of calculating 1.01 - 0.99 will be 0.020000000000000018 – a number very close to the expected, but not the same.

As integers, floating-point numbers have a certain size: 32 bits or 64 bits. Using a larger size increases the accuracy: the number of digits you can use for the calculation.

In addition to numbers, there are several other values, such as NaN (Not a number), for things like 0/0, as well as positive and negative infinity (+∞ and −∞).

There are two real types in Go: float32 and float64. They are often called single- and double-precision real numbers, respectively. When working with real numbers, it is often enough to use float32, however, if you want to work with more accurate numbers, you can also use float64.

Booleans

The Boolean type – bool (named after George Boole) – is a special type used to represent true or false. A variable of this type will occupy only one byte. Three logical operators are used with this type:

literal

explanation

&&

AND

||

OR

!

NOT

A bool type variable can take only two values: true or false.

var b bool = true

Example of operators:

var b1 = true
b2 := !b1      // false
b3 := b2 || b1 // false OR true => true
b4 := b2 && b3 // false AND true => false
b5 := b1 && b3 // true AND true => true

Rune and byte

There are also some interesting numeric data type names in Go; let's have a look at them:

  • rune is an alias for int32

  • byte is an alias for uint8

Golang doesn't have a separate data type for characters. It uses byte and rune to represent character values. The byte data type can represent ASCII characters, and the rune data type can store the decimal value of Unicode characters.

Complex numbers

Unlike some other languages, Go supports complex numbers: complex128and complex64. The complex64 type contains a 32-bit real and a 32-bit imaginary part and complex128 has 64-bit real and imaginary numbers. Go also provides a built-in function named complex for creating complex numbers. You'll need to use the complex function if you're creating a complex number with variables. Complex numbers allow all the basic arithmetic operations: + , , * , and /.

We will not dwell on this, but if you are still interested in it, you can refer to the documentation of the cmplx package.

Conclusion

There are several primitive types in Golang. In this topic, we've looked at them; in particular, we've covered the following built-in data types:

  • signed and unsigned integers;

  • floating-point numbers;

  • complex numbers;

  • the bool type;

  • runes and bytes.

Combining them, we can create programs using arithmetic and logical expressions.

132 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo