liveBook · Manning

Unsigned integer

As explained in Go in Action, Second Edition

An unsigned integer is an integer type that represents nonnegative whole-number values only.

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

int

Signed integers

32 or 64 bits, depending on your architecture

int8

Signed 8-bit integers

-128 to 127

int16

Signed 16-bit integers

-32,768 to 32,767

int32

Signed 32-bit integers

-2,147,483,648 to 2,147,483,647

int64

Signed 64-bit integers

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

uint

Unsigned integers

32 or 64 bits, depending on your architecture

uint8

Unsigned 8-bit integers

0 to 255

uint16

Unsigned 16-bit integers

0 to 65,535

uint32

Unsigned 32-bit integers

0 to 4,294,967,295

uint64

Unsigned 64-bit integers

0 to 18,446,744,073,709,551,615

byte

An alias for uint8

rune

An alias for int32

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

How Go in Action, Second Edition covers it

How deep it goes
Defines ranges and architecture-dependent sizing, then works through underflow and an argument-validation failure.
Examples
Uses zero-minus-one subtraction and a negative configuration value passed through a uint conversion.
What it assumes
Builds on whole-number values and Go variable declarations while defining the signed–unsigned distinction.
What you can do after
Apply it to choose an unsigned type where appropriate and validate nonnegative function input without relying on a uint parameter.
How it connects
Connects unsigned integers to signed integers, type conversions, compiler errors, wraparound, and input validation.

Also explained in

Compare how 9 books explain unsigned integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.