...

/

Elementary Types

Elementary Types

In this lesson, you'll study the different data types in detail.

The three main elementary types in Go are:

  • Boolean
  • Numeric
  • Character

Let’s discuss them in detail one by one.

svg viewer

Boolean type

The possible values of this type are the predefined constants true and false. For example:

var b bool = true

Numerical type

Integers and floating-point numbers

Go has architecture-dependent types such as int, uint, and uintptr. They have the appropriate length for the machine on which a program runs.

An int is a default signed type, which means it takes a size of 32 bit (4 bytes) on a 32-bit machine and 64 bit (8 bytes) on a 64-bit machine, and the same goes for uint (unsigned int). Meanwhile, uintptr is an unsigned integer large enough to store a bit pattern of any pointer.

The architecture independent types have a fixed size (in bits) indicated by their names. For integers:

  • 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)

For unsigned integers:

  • uint8 (with the alias byte, 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)

For floats:

  • float32 (± 1O−451O^{-45}
...