Numeric type aliases

February 22, 2023

Numeric Types

byte        alias for uint8
rune        alias for int32

To avoid portability issues all numeric types are defined types and thus distinct except byte, which is an alias for uint8, and rune, which is an alias for int32.

As with other aliases in Go, this means that the same type simply has two (or perhaps more) identifiers, which are completely interchangeable.

This means that byte is not a distinct type, simply backed by a uint8 type. It is the same type, with just a different identifier. This means that, for example, anywhere in Go code you see byte you could instead type uint8, or vice versa.

This is distinct from the case mentioned yesterday between int vs int32/int64, which may have the same underlying data structure, but are still distinct types.

So how can you tell which size your int is while running your program? Well, first, if you’re trying to determine this at runtime, first consider whether that’s really necessary. 😊 Can you design your program not to require that? But if you really must know, the unsafe.Sizeof function has your back. It will report the number of bytes used by a the argument’s zero value.

	fmt.Println(unsafe.Sizeof(int(0))) // 8 on a 64-bit system, 4 on a 32-bit system

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


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.


Go 1.24.0 is here!

Go 1.24 is released! Yay! That means a short re-visit to the Go spec, as we look at things that have changed. From the release notes we see that there’s really only one significant change: Changes to the language¶ Go 1.24 now fully supports generic type aliases: a type alias may be parameterized like a defined type. See the language spec for details. For now, the feature can be disabled by setting GOEXPERIMENT=noaliastypeparams; but the aliastypeparams setting will be removed for Go 1.


Uniqueness of type switch cases

Finally, the last important bit from this paragraph: Type switches … Cases then match actual types T against the dynamic type of the expression x. As with type assertions, x must be of interface type, but not a type parameter, and each non-interface type T listed in a case must implement the type of x. The types listed in the cases of a type switch must all be different. Today we’re examining this part:

Get daily content like this in your inbox!

Subscribe