Go Integers
Integers are whole numbers without a fractional part. Go offers several integer types that differ in size (how many bits they use) and whether they can be negative (signed) or not (unsigned).
Signed integers
| Type | Size | Range |
|---|---|---|
int8 |
8 bits | -128 to 127 |
int16 |
16 bits | -32,768 to 32,767 |
int32 |
32 bits | about ±2.1 billion |
int64 |
64 bits | about ±9.2 × 10¹⁸ |
int |
32 or 64 bits (platform) | 64-bit on all modern computers |
Unsigned integers
| Type | Size | Range |
|---|---|---|
uint8 (byte) |
8 bits | 0 to 255 |
uint16 |
16 bits | 0 to 65,535 |
uint32 |
32 bits | 0 to about 4.3 billion |
uint64 |
64 bits | 0 to about 1.8 × 10¹⁹ |
uint |
32 or 64 bits | platform dependent |
Tip: Use plain
intunless you have a specific reason not to — it is the type oflen(), loop counters and most standard-library functions. Use sized types for binary formats, network protocols or when memory really matters.
The math package has constants for the limits of each type:
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("int8: ", math.MinInt8, "to", math.MaxInt8)
fmt.Println("uint8: ", 0, "to", math.MaxUint8)
fmt.Println("int32: ", math.MinInt32, "to", math.MaxInt32)
fmt.Println("int64: ", math.MinInt64, "to", math.MaxInt64)
}int8: -128 to 127
uint8: 0 to 255
int32: -2147483648 to 2147483647
int64: -9223372036854775808 to 9223372036854775807
Writing integer literals
Go understands several notations, and you can use underscores to make long numbers readable.
package main
import "fmt"
func main() {
million := 1_000_000
binary := 0b1010 // 10
octal := 0o755 // 493
hex := 0xFF // 255
char := 'A' // a rune: 65
fmt.Println(million, binary, octal, hex, char)
}1000000 10 493 255 65
Integer arithmetic
The usual operators work: +, -, *, / and % (remainder). Integer division truncates toward zero — the fractional part is simply dropped.
package main
import "fmt"
func main() {
fmt.Println(17 / 5) // 3
fmt.Println(17 % 5) // 2
fmt.Println(-17 / 5) // -3, truncated toward zero
fmt.Println(-17 % 5) // -2, sign follows the dividend
}3
2
-3
-2
Dividing an integer by a variable that is zero causes a runtime panic (integer divide by zero); dividing by the constant 0 doesn't even compile.
Overflow wraps around
If a calculation goes past the maximum of its type at run time, the value silently wraps around — there is no error. Choose a type that is big enough for your data.
package main
import "fmt"
func main() {
var level uint8 = 250
level += 10 // 260 does not fit in a byte
fmt.Println(level)
var temp int8 = 127
temp++
fmt.Println(temp)
}4
-128
The compiler does catch overflow when the value is a constant: var b int8 = 200 fails with cannot use 200 (untyped int constant) as int8 value in variable declaration (overflows).
package main
func main() {
var b int8 = 200
_ = b
}# command-line-arguments
./example.go:4:15: cannot use 200 (untyped int constant) as int8 value in variable declaration (overflows)
Converting between integer types
Different integer types never mix automatically. Convert with T(x). Converting to a smaller type keeps only the low bits, so check the range first if the value might not fit.
package main
import "fmt"
func main() {
var a int32 = 70000
var b int64 = int64(a) * 2
c := int16(a) // 70000 does not fit in int16
fmt.Println(b, c)
}140000 4464
Test yourself
What is 7 / 2 in Go when both are integers?
- 3.5
- 3
- 4
- Compile error
What is the largest value a uint8 can hold?
- 127
- 128
- 255
- 256
What happens when an int8 variable holding 127 is incremented at run time?
- It panics
- It becomes 128
- It wraps around to -128
- It stays 127