Bytes, Runes & Sized Types

Bytes, Runes & Sized Types

The Full Type Lineup

You know int, float64, string, and bool. Go has more types for when you need precise control.

Sized Integers

The plain int type adapts to your machine (usually 64-bit). But sometimes you need a specific size:

Type Bits Range
int8 8 -128 to 127
int16 16 -32,768 to 32,767
int32 32 ±2 billion
int64 64 ±9 quintillion

Unsigned Integers

When you know a number can't be negative, use unsigned types. They trade the negative range for double the positive range:

Type Bits Range
uint8 8 0 to 255
uint16 16 0 to 65,535
uint32 32 0 to ~4 billion
uint64 64 0 to ~18 quintillion

The Special Ones

Type What it is When to use
byte Alias for uint8 Raw data, file contents, network bytes
rune Alias for int32 Unicode characters
float32 Lower precision float64 Graphics, specific APIs

A rune holds a character as a number. 'A' is 65, '日' is 26085:

example.go
var b byte = 255       // same as uint8
var r rune = '日'       // same as int32
fmt.Println(b)         // 255
fmt.Println(string(r)) // 日
>_Exercise

Exercise: Uppercase

Since runes are just numbers, you can do math on them. In Unicode, lowercase 'a' is 97 and uppercase 'A' is 65. The difference is always 32.

Fill in toUpper. If the rune is a lowercase letter (between 'a' and 'z'), subtract 32 to make it uppercase. Otherwise return it unchanged.

Examples:

  • toUpper('g')'G'
  • toUpper('z')'Z'
  • toUpper('A')'A' (already uppercase)
  • toUpper('3')'3' (not a letter)
Stuck? Reveal a hint to help you.
Hints (0/3)
Key Takeaway
Next UpWhat strings really are under the hood.