Skip to content

Variables & Types

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: 0
var active bool // zero value: false
var count int = 10 // explicit initial value

Inside 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).

Every type in Go has a zero value — the value a variable holds before any assignment. There is no concept of an uninitialised variable.

TypeZero value
int, int8, int16, int32, int640
uint, uint8, uint16, uint32, uint640
float32, float640.0
string""
boolfalse
pointer, slice, map, channel, functionnil

This guarantee means you can always use a variable safely after declaration, even without an explicit initialiser.

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 integer
var u uint = 100 // platform-sized unsigned integer
var i32 int32 = 2_147_483_647
var f64 float64 = 3.14159
var b byte = 255 // alias for uint8
var r rune = 'A' // alias for int32; holds a Unicode code point
var s string = "hello"
var ok bool = true

rune and byte are built-in type aliases, not distinct types. rune is int32; byte is uint8.

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.

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 = 7
var y float64 = 2.5
// sum := x + y // compile error: mismatched types int and float64
sum := float64(x) + y // explicit conversion: 9.5
var n int = int(sum) // truncates: 9
package 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))
}
What is the zero value of a string variable in Go?
Given `const ( A = iota; B; C )`, what is the value of C?
Which of the following correctly converts an int to float64 in Go?
Where can the short declaration operator := NOT be used?