Fixed size integers
Fixed size integers require exact amount of memory. Using a smaller integer type "here and there" for single variables, you will only gain a tiny amount of memory, if any. Also when used as types of struct fields, you again might not gain anything due to implicit paddings.
The memory gain may be noticeable and considerable when you use fixed size integers as the element type of (big) slices or arrays.
Another (maybe more important) reason to use fixed size integers may be to communicate what you store in them. You could just as well use int32
or int64
type to store bytes, but being an obvious waste, they don't communicate the valid range of the data stored in them.
Another point is efficiency. You could always use int64
in place of other signed integer types, but on certain architectures performing operations on int64
might require multiple register operations and thus being considerably slower. Also the rune
type (alias for int32
) clearly communicates that you intend to use it for unicode codepoints.
Another point is consistency. If you use int32
to model something in one place, you should stick to it and use the same type everywhere. This is more important in Go than in other languages, because Go's type system is strict (stricter than most other language's), meaning if you have a value of type int32
, you can't assign it to a variable of int64
type and vice versa without explicit conversion.
int
vs fixed size integers
The types int
and uint
are not fixed sizes, but according to Spec: Numeric types:
uint either 32 or 64 bits
int same size as uint
When you use int
, the compiler may produce more optimized code when targeting different architectures. Usually int
is 32-bit when targeting 32-bit architectures, and 64-bit when targeting 64-bit architectures. What this means is that the size of int
will match the target architecture's register size, so integer operations can be efficiently carried out with single register operations. If you use int64
for example, that may require to perform multiple (register) operations to carry out a single integer operation on a 32-bit architecture.
I like to think of int
being an integer type that is used to describe and communicate certain parts or components of Go's runtime data structures in the best way it sees fits. For example to index arrays or slices, or to describe their sizes, int
is the "natural" type to use.
Spec: Length and capactiy:
The built-in functions len
and cap
take arguments of various types and return a result of type int
. The implementation guarantees that the result always fits into an int
. For example indexing slices or array, or to describe their length and capacity, int
is the "natural" type recommended or enforced.
When using the for range
statement with at least one iteration variable on arrays, slices or string
values, the iteration variable (the "index") will be of type int
.
Also note that a proposal has been presented by Rob Pike for Go 2 to change int
to arbitrary precision. proposal: spec: change int to be arbitrary precision