Go has many built in types that are found in other programming languages. It also supports the creation of custom types.

Each type has a default zero value that is used when a variable is declared but not yet assigned a value.

Boolean

Booleans are true or false values. The zero value for a boolean is false.

go

// Declare a boolean with its zero value
var stuff bool // false

// Declare a boolean and set its value
things := true
    

Integer

Go supports a number of both of signed and unsigned integer types. They range in from one to four bytes in size.

The default zero value for all integer types is 0

A byte is an alias for uint8. In practice byte is preferred over uint8

On a 32-bit CPU an int/uint is an int32/uint32. On most 64-bit CPU's an int/uint is an int64/uint64

The most common integer types are listed below.

Name Value Range
int8 -128 to 127
int16 -32768 to 32767
int32 -2147483648 to 2147483647
int64 -9223372036854775808 to 9223372036854775807
uint8 0 to 255
uint16 0 to 65536
uint32 0 to 4294967295
uint64 0 to 18446744073709551615

Float

Go supports two types of floats; float32 and float64

The default zero value for a float is 0

Warning

A floating point number in Go cannot represent an exact decimal value. They should not be used for anything that requires exact decimal precision.

Strings

Strings in Go are represented with the double quote characters "".

The default zero value for a string is an empty string "".

Strings in Go are immutable, once they are assigned the value cannot be changed.

Considerations

  • Go does not have any automatic type promotion/conversion
  • If required, types must be explicitly converted
  • There are no truthy/falsy values in Go

Tags

golang