Go integers store whole numbers within a range determined by their type. They are used for arithmetic, array and slice indices, and arguments to standard-library functions. Start with int for most work: it handles negative values and matches the type used by len, cap, and for-range indices. section 3.1 section 3.1.1
Choose a more specific type when the value or an API requires it:
- Signed int8 through int64 and unsigned uint8 through uint64 have fixed widths. int and uint are 32 or 64 bits, depending on the target architecture. section 3.1
- Use int64 when its range is needed, or when working extensively with APIs that take it, such as those handling timestamps or file offsets. Avoid smaller signed types solely to save memory; benchmark before optimizing. section 3.1.1
- byte aliases uint8; rune aliases int32. Their names clarify the context in which an integer is used. section 3.1
An explicitly declared int starts at zero; i := 3 creates an int. Write uint64(4) when a short declaration needs that type. Integer literals can use decimal, hexadecimal (0x), octal (0 or 0o), or binary (0b) notation. Digit-separating underscores change readability, not value. section 2.4.8 section 3.1
Validate before converting
Unsigned types do not enforce a positive-input rule. Subtracting 1 from a uint64 holding zero produces 18446744073709551615; converting a negative configuration value to uint can likewise yield a large value. Keep the input as int, check whether it is negative, and return an error before using it as a nonnegative value. section 3.1.1
Table 3.1 Predeclared integer types
| Type name |
Description |
Value vange |
|---|---|---|
|
|
Signed integers |
32 or 64 bits, depending on your architecture |
|
|
Signed 8-bit integers |
-128 to 127 |
|
|
Signed 16-bit integers |
-32,768 to 32,767 |
|
|
Signed 32-bit integers |
-2,147,483,648 to 2,147,483,647 |
|
|
Signed 64-bit integers |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
|
|
Unsigned integers |
32 or 64 bits, depending on your architecture |
|
|
Unsigned 8-bit integers |
0 to 255 |
|
|
Unsigned 16-bit integers |
0 to 65,535 |
|
|
Unsigned 32-bit integers |
0 to 4,294,967,295 |
|
|
Unsigned 64-bit integers |
0 to 18,446,744,073,709,551,615 |
|
|
An alias for |
|
|
|
An alias for |
Compare the predeclared types and their ranges when a value needs a fixed width or a larger range than the default int. section 3.1














