Numerical Types
Go has most of the well-known types such as int
. The int
type has theappropriate length for your machine, meaning that on a 32-bit machine it is 32bits and on a 64-bit machine it is 64 bits. Note: an int
is either 32 or 64bits, no other values are defined. Same goes for uint
, the unsigned int.
If you want to be explicit about the length, you can have that too, withint32
, or uint32
. The full list for (signed and unsigned) integers isint8
, int16
, int32
, int64
and byte
, uint8
, uint16
, uint32
,uint64
, with byte
being an alias for uint8
. For floating point valuesthere is float32
and float64
(there is no float
type). A 64 bit integer orfloating point value is always 64 bit, also on 32 bit architectures.
Note that these types are all distinct and assigning variables which mix thesetypes is a compiler error, like in the following code:
package main
func main() {
var a int
var b int32
b = a + a
b = b + 5
}
We declare two different integers, a and b where a is an int
and b is anint32
. We want to set b to the sum of a and a. This fails and gives the error:cannot use a + a (type int) as type int32 in assignment
. Adding the constant5 to b does succeed, because constants are not typed.