Beginner Fundamentals

Variables

Variables store values that your program can read and change. Go offers a few ways to declare them, all type-safe.

Using var

The var keyword declares a variable. You can give an explicit type or let Go infer it from the value.

var age int = 30
var name = "Ada"   // type inferred as string
var active bool    // declared without a value

Short declaration

Inside functions you can use := to declare and assign in one step. This is the most common form.

count := 10
message := "Hello"
price := 9.99

Note: := only works inside functions, not at package level.

Zero values

A variable declared without a value gets its type’s zero value, never a random one.

var n int       // 0
var f float64   // 0
var s string    // "" (empty string)
var b bool      // false

Multiple variables

You can declare several variables at once.

var x, y = 1, 2
a, b := "left", "right"
fmt.Println(x, y, a, b)