Variables, Types, Constants & Pointers

var and :=, zero values, explicit conversions, iota, and pointers without pointer arithmetic.

Go's basics are deliberately simple: every variable has a zero value, conversions are always explicit, and pointers exist but cannot be used for arithmetic.

The basics

var name type = value is the full form; := is the short form inside functions.

Unassigned variables hold their zero value: "", 0, false, nil.

int(small) converts explicitly; uint8 arithmetic wraps (250 + 10 = 4).

iota numbers constants 0, 1, 2, 3 — Go's way of writing enums.

x, y = y, x swaps; &n takes an address and *ptr follows it.

Types

var total int64 = count + big // count is int var total int64 = int64(count) + big

Go never converts between numeric types implicitly, even int to int64.

What is the zero value of a pointer, slice, map and string?

nil, nil, nil and the empty string "".