Variables and types
var, :=, zero values, and Go's basic types — plus const and multiple assignment.
- Declare variables with var and with the short := syntax
- Explain Go's zero values and why they matter
- Name the common basic types — int, float64, string, bool
- Use const for values that never change
- Assign multiple variables in a single statement
In the Fundamentals track, variables are described as names bound to values in memory. Go takes that idea and adds a guarantee you won't find in many other languages: every variable starts with a well-defined zero value. There is no such thing as an uninitialised variable in Go. That removes an entire category of bugs from the start.
var and short variable declaration
Go has two ways to declare a variable. The long form uses var:
var name string = "Alice"
var age int = 30When the value makes the type obvious, you can let the compiler infer it:
var name = "Alice" // inferred as string
var age = 30 // inferred as intInside a function, the short variable declaration := is more idiomatic:
func main() {
name := "Alice"
age := 30
fmt.Println(name, age)
}:= declares and assigns in one step. The compiler infers the type from the right-hand side, exactly as var with inference does — it's just fewer characters.
:= only works inside functions. At the package level (outside any function) you must use var. Also, := requires at least one new variable on the left — x := 5; x := 6 is a compile error because x is already declared.
Zero values
Every type in Go has a zero value — the value a variable holds if you declare it without an initialiser:
| Type | Zero value |
|---|---|
int, float64, etc. | 0 |
string | "" (empty string) |
bool | false |
| pointer, slice, map, function | nil |
var count int // 0
var label string // ""
var active bool // falseThis matters because you can always use a variable safely. A function that returns an int but hits an error path doesn't need a special "not set" sentinel — it just returns 0 by default, and you handle the error separately.
Zero values are deliberate. Many languages leave uninitialized memory as garbage. Go initialises everything. This makes concurrent programs safer and eliminates a common source of hard-to-reproduce bugs in C and C++. It's one of the quiet wins in Go's design.
Basic types
Integers — Go has sized integer types (int8, int16, int32, int64) and their unsigned counterparts (uint8, etc.). In practice you almost always use plain int, which is 64-bit on modern 64-bit systems:
var score int = 42
var population int = 8_000_000_000 // underscores for readabilityFloating-point — float64 is the default and what you should use for decimal numbers. float32 exists but is rarely needed:
pi := 3.14159String — a sequence of bytes, usually UTF-8 text. String literals use double quotes:
greeting := "Hello, Go!"Strings are immutable — you can't change individual bytes in place, but you can produce new strings.
Bool — exactly true or false:
isReady := trueMultiple assignment
Go lets you assign multiple variables in one statement, which is idiomatic for functions that return more than one value (more on that in the next lesson):
x, y := 10, 20
fmt.Println(x, y) // 10 20You can also swap two variables without a temporary:
x, y = y, x
fmt.Println(x, y) // 20 10const
For values that never change, use const. Constants are evaluated at compile time and can be used anywhere a literal value is valid:
const Pi = 3.14159
const AppName = "MyApp"
const MaxRetries = 3A constant block groups related constants:
const (
StatusOK = 200
StatusNotFound = 404
StatusError = 500
)Prefer const over var for fixed values. It signals intent clearly — this value will never change — and the compiler can catch accidental assignments. It also allows the compiler to optimise more aggressively.
Check your understanding
Knowledge check
- 1.What is the value of count after
var count int? - 2.Where can the short variable declaration := be used?
- 3.In Go, you can modify individual characters of a string in place.
Do it yourself
In your module from the last lesson, replace the main body with:
func main() {
var city string
var population int
city = "Gopher City"
population = 1_200_000
fmt.Println(city, "population:", population)
ratio := 0.42
fmt.Printf("%.2f%%\n", ratio*100)
}Run go run main.go. Then add a const for the city name and see that the compiler prevents you from reassigning it.
Where to go next
You know how to declare variables, what zero values guarantee, and what the core types look like. Next: functions — how to package logic into reusable pieces, and Go's most distinctive feature: multiple return values.