Variables & Types
Declaring variables
Section titled “Declaring variables”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.
Basic types
Section titled “Basic types”| 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.
Zero values
Section titled “Zero values”Unlike some languages, an uninitialized variable in Go is never garbage — every type has a zero value:
var i int // 0var f float64 // 0.0var s string // ""var b bool // falseThis eliminates a whole class of “uninitialized variable” bugs by construction.
Constants
Section titled “Constants”const Pi = 3.14159const MaxRetries = 3Constants 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.
Type conversion
Section titled “Type conversion”Go never converts types implicitly — even int32 to int64 requires an explicit conversion:
var i int = 42var 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.