Basic Types
The basic types
The ones you'll use constantly:
var i int // platform-sized integer (64-bit on virtually everything today)
var f float64 // 64-bit floating point
var b bool // true or false
var s string // UTF-8 text, immutableThere are also sized integer types (int8, int32, uint64, ...) for when the exact width matters — packing bytes for a network protocol, say — but reach for plain int by default.
A variable declared with var and no value gets its type's zero value, not null/None/undefined:
var count int // 0
var label string // ""
var ready bool // falseEvery type has exactly one zero value:
| Type | Zero value |
|---|---|
int, int8…int64, uint…uint64, byte, rune | 0 |
float32, float64 | 0 |
complex64, complex128 | 0+0i |
bool | false |
string | "" (empty, not nil) |
pointer (*T) | nil |
slice ([]T) | nil (behaves as an empty, length-0 slice) |
map (map[K]V) | nil (readable as empty; writing to it panics) |
channel (chan T) | nil |
| function | nil |
| interface | nil |
| struct | each field set to its own zero value |
array ([N]T) | every element set to T's zero value |
This is deliberate: Go doesn't have a universal nil-for-anything the way Python has None for every variable. Only a handful of types (pointers, slices, maps, interfaces, channels, functions) can be nil; an int or string always has a real, usable zero value, so there's no "forgot to initialize it, now it's null and everything downstream throws" class of bug for these types.