Skip to the workspace
Pebellslearn by building
Data Types  ›  Lesson

Integers, Signed and Unsigned

Integers (Signed, Unsigned) 14 minGo has signed and unsigned integers of fixed sizes, never mixes them without a conversion, divides them by truncating toward zero, and wraps around silently when a value overflows at run time.

golang / Integers, Signed and Unsigned

Explore each scene at your own pace.

01 · The rule

The rule

Integers, Signed and Unsigned

Go has signed and unsigned integers of fixed sizes, never mixes them without a conversion, divides them by truncating toward zero, and wraps around silently when a value overflows at run time.

Choose an integer representation from its range and inspect conversions between signedness.

tally counts things: words, lines, letters. Counting means integers, and Go offers ten of them. Most of the time the answer is simply int, but you need to know why, and what happens at the edges.

The rule

Use int by default; sizes are fixed, types never mix, and overflow wraps.

Signed (can be negative) Unsigned (0 and up)
int8 −128 to 127 uint8 0 to 255 (alias byte)
int16, int32 (alias rune), int64 uint16, uint32, uint64
int — 64 bits on 64-bit systems uint, uintptr

Default to int. len returns an int, loop counters are int, and an untyped whole number like total := 0 becomes int. Reach for sized types only when a file format, protocol or huge array demands them.

Types never mix. An int and an int64 cannot be added until you convert one: total + int(extra). This feels strict, but it makes every size change visible.

Integer division truncates toward zero. 17 / 4 is 4, and -7 / 2 is −3, not −4. The remainder keeps the sign of the left side: -7 % 2 is −1.

Overflow wraps at run time.

var b uint8 = 255
b++
fmt.Println(b) // 0

var s int8 = 127
s++
fmt.Println(s) // -128

No panic, no warning. The compiler only catches overflow in constants: var b uint8 = 256 is a compile error.

Literals can help readability: 1_000_000, 0xFF (hex), 0b1010 (binary), 0o755 (octal).

When should you pick an unsigned type on purpose? When the data is raw bytes (byte, []byte), bit patterns such as flags or hashes, or a format that says "unsigned 32-bit". For counting and arithmetic, stay with int.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.