Go Notes: Types
Published: 2019-12-01
Intro
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.
// Declare a boolean with its zero value
var stuff bool // false
// Declare a boolean and set its value
things := trueInteger
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.
Float
Go supports two types of floats; float32 and float64
The default zero value for a float is 0
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