Go’s basic types are
- bool
- string
- int int8 int16 int32 int64
- uint uint8 uint16 uint32 uint64 uintptr
- byte // alias for uint8
- rune // alias for int32, represents a Unicode code point
- float32 float64
- complex64 complex128
The int
, uint
, and uintptr
types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.
Strings are made of runes
which are unicode code points. If you take the length of a string, you might not get what you expect. The following prints 3:
fmt.Println(len("椒"))
If you iterate over a string using range
, you’ll get runes, not bytes.
Strings and byte arrays are closely related. We can easily convert one to the other:
stra := "the spice must flow"
byts := []byte(stra)
strb := string(byts)
Do note that when you use []byte(X)
or string(X)
, you’re creating a copy of the data. This is necessary because strings are immutable.
Type conversions
The expression T(v)
converts the value v
to the type T
. Unlike in C, Go assignment between items of different type requires an explicit conversion.
Type inference
When declaring a variable without specifying an explicit type (either by using the :=
syntax or var =
expression syntax), the variable’s type is inferred from the value on the right hand side. When the right hand side of the declaration is typed, the new variable is of that same type. But when the right hand side contains an untyped numeric constant, the new variable may be an int
, float64
, or complex128
depending on the precision of the constant.