liveBook · Manning

Integer

As explained in Go in Action, Second Edition

An integer type represents whole-number values without a fractional component.

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

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 predeclared types and their ranges when a value needs a fixed width or a larger range than the default int. section 3.1

How Go in Action, Second Edition covers it

How deep it goes
Explains signedness, widths, literals, and unsigned wraparound; recommends benchmarking rather than supplying performance measurements.
Examples
Demonstrates equivalent literals in several bases, unsigned underflow, and rejection of a negative configuration value.
What it assumes
Assumes familiarity with Go variables and function calls while explaining integer declarations, inferred types, and zero values.
What you can do after
Practice/perform it: choose int by default, select int64 when its range is needed, and validate negative input before unsigned conversion.
How it connects
Connects integer choice to slice indices, len, cap, type conversions, and configuration validation.

Also explained in

Compare how 15 books explain 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.