Junior
How many bytes does it take to store a number?
sobes.tech AI
Answer from AI
The size of a number in Go depends on its type.
| Data type | Size (bytes) | Value range |
|---|---|---|
int |
Depends on architecture (32 or 64 bits) | Depends on architecture |
uint |
Depends on architecture (32 or 64 bits) | Depends on architecture |
int8 |
1 | -128 to 127 |
uint8 |
1 | 0 to 255 |
int16 |
2 | -32768 to 32767 |
uint16 |
2 | 0 to 65535 |
int32 |
4 | -2147483648 to 2147483647 |
uint32 |
4 | 0 to 4294967295 |
int64 |
8 | -9223372036854775808 to 9223372036854775807 |
uint64 |
8 | 0 to 18446744073709551615 |
float32 |
4 | About ±3.4e38 |
float64 |
8 | About ±1.8e308 |
complex64 |
8 | Two float32 values (real and imaginary parts) |
complex128 |
16 | Two float64 values (real and imaginary parts) |
byte |
1 | Alias for uint8 |
rune |
4 | Alias for int32 (Unicode code point) |
Example of determining the size of a type on a specific platform:
import (
"fmt"
"unsafe"
)
func main() {
// Size of int and uint depends on processor architecture
fmt.Printf("Size of int: %d bytes\n", unsafe.Sizeof(int(0)))
fmt.Printf("Size of uint: %d bytes\n", unsafe.Sizeof(uint(0)))
// Sizes of fixed types are constant
fmt.Printf("Size of int8: %d bytes\n", unsafe.Sizeof(int8(0)))
fmt.Printf("Size of float64: %d bytes\n", unsafe.Sizeof(float64(0)))
}