Variables & Types
Declaring variables
Section titled “Declaring variables”Go has two ways to declare a variable. The var keyword works at both package scope and function scope:
var name string // zero value: ""var age int // zero value: 0var active bool // zero value: falsevar count int = 10 // explicit initial valueInside a function, the short declaration operator := infers the type from the right-hand side and is the idiomatic choice:
func main() { name := "Alice" // string score := 98.6 // float64 done := false // bool}:= cannot be used at package scope — only var works there. You also cannot use := if every variable on the left side was already declared (at least one must be new).
Zero values
Section titled “Zero values”Every type in Go has a zero value — the value a variable holds before any assignment. There is no concept of an uninitialised variable.
| Type | Zero value |
|---|---|
int, int8, int16, int32, int64 | 0 |
uint, uint8, uint16, uint32, uint64 | 0 |
float32, float64 | 0.0 |
string | "" |
bool | false |
| pointer, slice, map, channel, function | nil |
This guarantee means you can always use a variable safely after declaration, even without an explicit initialiser.
Basic types
Section titled “Basic types”Go’s numeric types are explicit about their size. The unsized int and uint match the platform word size (32 or 64 bits).
var i int = -100 // platform-sized signed integervar u uint = 100 // platform-sized unsigned integervar i32 int32 = 2_147_483_647var f64 float64 = 3.14159var b byte = 255 // alias for uint8var r rune = 'A' // alias for int32; holds a Unicode code pointvar s string = "hello"var ok bool = truerune and byte are built-in type aliases, not distinct types. rune is int32; byte is uint8.
Constants and iota
Section titled “Constants and iota”Constants are declared with const and evaluated at compile time. A const block with iota creates auto-incrementing integer sequences — the idiomatic Go way to define enumerations:
const Pi = 3.14159
type Direction int
const ( North Direction = iota // 0 East // 1 South // 2 West // 3)iota resets to zero at the start of each const block and increments by one for each constant specification within the block.
Explicit type conversions
Section titled “Explicit type conversions”Go has no implicit numeric conversions. To use a value of one numeric type where another is expected, you must convert explicitly using the target type as a function:
var x int = 7var y float64 = 2.5
// sum := x + y // compile error: mismatched types int and float64sum := float64(x) + y // explicit conversion: 9.5
var n int = int(sum) // truncates: 9package main
import "fmt"
type Weekday int
const ( Monday Weekday = iota + 1 Tuesday Wednesday Thursday Friday Saturday Sunday)
func main() { // Zero values var i int var s string var b bool fmt.Println("Zero int:", i) fmt.Println("Zero string:", s == "") fmt.Println("Zero bool:", b)
// Short declaration name := "Go" version := 1.22 fmt.Println("Language:", name, "Version:", version)
// iota enum fmt.Println("Monday =", Monday) fmt.Println("Wednesday =", Wednesday) fmt.Println("Sunday =", Sunday)
// Explicit type conversion meters := 100 kilometers := float64(meters) / 1000.0 fmt.Println("100 meters =", kilometers, "km")
// rune vs byte ch := 'Z' fmt.Println("rune 'Z' as int32:", int32(ch)) fmt.Println("byte value:", byte(ch))}Loading Go runtime (first run only, ~8 MB)…