Golang Basics - Types
Variables in Go are static, and strongly typed. Go has a few primitive types, of which all other types are derived. They are:
There are multiple integer types, depending on the number of bits, and whether it's signed or not:
// Signed integers int int8 int16 int32 int64 // Unsigned integers uint uint8 uint16 uint32 uint64 uintptr // Integer aliases byte rune
The signed and unsigned integer types store integers that contain the number of bits denoted by the type. For example,
int32 is a 32 bit integer. The
uint types store integers with the nuber of bits dependent on the system for which the code is compiled.
byte type is an alias for
uint8. Since it represents 8 bits, it can also be represented by an 8-bit integer value.
rune type is used to represent individual characters, or code points, in a string. Since each code point can be represented by 32 bits, it can be aliased to
You can read more about runes on the official blog
You can denote 32-bit and 64-bit floating point numbers by using the
float64 types respectively.
Go also has provisions for complex values (
x + iy) with the
complex128 types which hold pairs of
float64 values for the real and imaginary parts of the complex number.
// The default type when declared with `:=` is float64 pi := 3.1412 // A float32 type can be initialized like this var pi32 float32 = 3.1412 // Default complex type is `complex128` cmpx := 2 + 3i // A complex64 type can be initialized like this var cmpx64 complex64 = 2 + 3i
Integers and float types that are not assigned a value defualt to
A string is in effect a collection of characters (in Go terms these are called runes), and denoted by the type
A string literal can be defined with
name := "John Doe" var anotherName string anotherName = "Jane Doe" description := `A string constructed with backticks can have new-lines included: ...Like this!`
A string without an assigned value defaults to an empty string
bool type is used to denote the boolean values:
flag := true var toggle bool
A boolean without an assigned value defaults to
📖 Read next: Structs →
Liked this article? Share it on: