Skip to content

Variables & Types

Go gives you two ways to declare a variable:

var name string = "Gopher"
age := 12 // short form, type inferred

:= is by far the more common style inside functions. var is used at package level (where := isn’t allowed) or when you want to be explicit about the type, or want the zero value instead of an initial value.

Category Types
Integers int, int8, int16, int32, int64, and unsigned uint* variants
Floats float32, float64
Text string, byte (alias for uint8), rune (alias for int32, a Unicode code point)
Boolean bool

int and uint are 64 bits on virtually every modern platform — use them unless you have a specific reason (binary formats, memory-constrained code) to pick a fixed-width type.

Unlike some languages, an uninitialized variable in Go is never garbage — every type has a zero value:

var i int // 0
var f float64 // 0.0
var s string // ""
var b bool // false

This eliminates a whole class of “uninitialized variable” bugs by construction.

const Pi = 3.14159
const MaxRetries = 3

Constants are evaluated at compile time and can’t be reassigned. Go also supports iota for auto-incrementing groups of related constants — you’ll see that used for enums in the Structs & Pointers chapter.

Go never converts types implicitly — even int32 to int64 requires an explicit conversion:

var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

This is stricter than languages like C or JavaScript, but it prevents silent precision loss and surprising bugs.