Go Variables, Static Types, and Zero Values
How Go stores numbers, text, and booleans with static types, when to use := vs var, and why Go never leaves a variable full of garbage.
You'll learn how Go locks every variable to a specific type at compile time, how := saves typing without losing safety, and what "zero values" mean. Assumes you have Go installed and know how to run go run ..
1. Static types: one label, one kind of thing
In Python or JavaScript, a variable named thing can hold the number 7 on line 1 and the word "seven" on line 2. That freedom is convenient until a function expecting a number gets "seven" in production at 3 a.m.
Go is statically typed. Every variable has a fixed type—and once a variable is an int (whole number), it stays an int forever. The compiler checks every assignment before your program ever runs.
Inside a function, the fastest way to create a variable is := (short variable declaration):
package main
import "fmt"
func main() {
name := "Gerald" // string (text)
daysLeft := 12 // int (whole number)
caffeine := 0.75 // float64 (decimal)
isAwake := true // bool (true or false)
fmt.Println(name, daysLeft, caffeine, isAwake)
}Gerald 12 0.75 trueGo looks at "Gerald" on the right, realizes it's text, and locks name to type string automatically. Try changing daysLeft to text later:
daysLeft = "twelve"cannot use "twelve" (untyped string constant) as int value in assignmentNotice also: the first time you create daysLeft, you use := (declare + assign). Every time you update it afterward, you use = (assign only).
2. When to use var instead of :=
var is the longhand way to declare a variable. You need var in two situations:
- Outside any function (at package level), where
:=is not allowed. - When you don't have a starting value yet, and want Go's safe default.
package main
import "fmt"
var appName = "CodeHeart" // package-level: must use var
func main() {
var retries int
var note string
var ok bool
fmt.Printf("%s -> retries=%d note=%q ok=%v\n", appName, retries, note, ok)
}CodeHeart -> retries=0 note="" ok=false3. Zero values: no uninitialized garbage
In older languages like C, declaring int retries; without a value leaves whatever random leftover bytes were sitting in RAM. In Go, every uninitialized variable automatically gets its zero value:
- Numbers (
int,float64) ->0 - Booleans (
bool) ->false - Strings (
string) ->""(empty string)
That means you never have to worry about a variable holding undefined or random memory.
Declare count := 0 inside main(), then try writing count := 5 on the next line. Read the error (no new variables on left side of :=) and fix it by changing the second := to =.
4. Go refuses to guess type conversions
Go will not silently mix int and float64—even for simple math:
a := 5
b := 2.5
fmt.Println(a + b)invalid operation: a + b (mismatched types int and float64)You must convert explicitly: float64(a) + b gives 7.5. Annoying for five seconds; saves you from subtle rounding bugs for five years.
Common mistakes
| Error | What it means | Fix |
|---|---|---|
no new variables on left side of := | Used := on a variable that already exists | Use = to reassign |
x declared and not used | Created a local variable and never read it | Use x or delete it |
mismatched types int and float64 | Mixed whole numbers and decimals in math | Wrap with float64(x) or int(y) |
syntax error: non-declaration statement outside function body | Used := at package level | Use var x = ... outside functions |
Practice
- What is the zero value of a
stringand aboolin Go? - Why does
x := 10; x = 3.5fail to compile?
Answers:
""(empty string) andfalse.xwas locked to typeintonx := 10, and3.5is a decimal (float64), which doesn't fit in anint.
What you learned
:=declares and infers a variable's static type inside functions;=reassigns it.varis required outside functions and when declaring a variable with its zero value (0,false,"").- Go never silently converts between types like
intandfloat64.