Architecture-independent Numeric Types

February 20, 2023

Go provides several predeclared numeric types. Most of them have a fixed size, regardless of the architecture the program is compiled for or running on.

Numeric Types

An integer, floating-point, or complex type represents the set of integer, floating-point, or complex values, respectively. They are collectively called numeric types. The predeclared architecture-independent numeric types are:

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

The value of an n-bit integer is n bits wide and represented using two’s complement arithmetic.

This gives us a ton of flexibility, built right into the language. Sometimes you need even larger numbers, though, and then the standard library’s math/big package can help.

If you’re dealing with currencies, you may be tempted to use float64 or even float32, but please don’t! IEEE-754 floating point numbers are not precise. And not just for really big or really small numbers. You will introduce bugs into your software by using floating point values for currency addition or other math. Instead, either use an integer type, and treat the right most digits as decimal places (usually 2 decimal places, but it depends on your currency), or use a library specifically intended for handling currency math (I don’t actually know of a good one, or I’d link to it here).

More on byte and rune later this week.

Quotes from The Go Programming Language Specification, Version of January 19, 2023


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Lexical elements: Floating-point literals

Ah, who doesn’t love floating point numbers? Go gives us two ways to define floating point number literals: in decimal, or in hexidecimal. I have never been tempted to write a floating point number literal in hexidecimal. I imagine this is mostly used by those interested in precision control of the IEEE 754 values as understood by the floating point implementation. (If this is something you’ve ever used, I’d love to hear from you: What was your context?


Lexical elements: Integer literals

Integer literals Ah integer literals! Now we’re starting to get into the meat of the language. Go lets us express integers in four bases. 10 (decimal) is by far the most common, followed by base 16 (hexidecimal). Base 8 (octal) and base 2 (binary) are also supported, but seen fairly rarely. Here’s how the spec describes it: An integer literal is a sequence of digits representing an integer constant. An optional prefix sets a non-decimal base: 0b or 0B for binary, 0, 0o, or 0O for octal, and 0x or 0X for hexadecimal.


Integer context keys

We’re looking at different types for context keys. So far, we’ve looked at empty structs (struct{}), and found it to be less than ideal. Today, let’s consider integer context keys. This seems handy, right? type contextKey int const ( KeyUserID int = iota KeyTransactionID KeyMessageID . . . KeyFoo ) Let’s see how it stacks up to our 5 criteria: Must be comparable. ✅ No problem! Use minimal membory. ✅ int doesn’t have as small a memory footprint as struct{}’s zero bytes, but it’s still pretty small.

Get daily content like this in your inbox!

Subscribe