An unsigned integer stores whole numbers from 0 upward. Go names these types uint, uint8, uint16, uint32, and uint64. Unlike a corresponding signed type, an unsigned type does not reserve a bit for negative values, so its nonnegative range is larger. section 3.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 ranges before choosing a type: uint8 holds 0–255, while uint64 holds 0–18,446,744,073,709,551,615. Unlike the fixed-width types, uint is 32 or 64 bits depending on the architecture. section 3.1
A subtraction that wraps
With `var u uint64`, the value starts at 0. After `u = u - 1`, printing `u` yields `18446744073709551615`, not -1: subtraction has wrapped to the type's maximum value. This edge case is easy to encounter because zero is the bottom of every unsigned range. section 3.1.1
Do not use a uint parameter merely to enforce a nonnegative input. A negative literal passed directly is rejected by the compiler, but converting a negative int obtained elsewhere can produce a large unsigned value instead. If rejection is the goal, accept an int, check `i < 0`, and return an error. section 3.1.1 section 3.1.1
Listing 3.6 Calling a function that takes a uint with a converted negative value
func main() {
// User has a signed integer value from somewhere else that is
// unintentionally negative.
configVal := GetConfigValue() // returns int(-1)
// They need to convert it to call your function, but wait...
OnlyPositive(uint(configVal))
}
// output: positive value is 18446744073709551615
Follow the configuration-value example to see why a uint parameter alone does not validate input: converting an int value of -1 lets the call proceed with a large value. section 3.1.1








