Type sizes
Go lets you control memory with different integer sizes:
| Type | Bytes | Range |
|---|---|---|
int8 | 1 | -128 to 127 |
int16 | 2 | -32,768 to 32,767 |
int32 | 4 | ±2 billion |
int64 | 8 | huge |
Choose the smallest type that holds your values to save memory in large collections.
Type conversion
Go does not convert automatically between numeric types. You must convert explicitly:
i := 42
f := float64(i) // int to float
j := int(f) // float back to int (truncates)
int(f) truncates toward zero, int(3.99) is 3, not 4. Always convert deliberately.