Theory
Zero values & blocks
What happens if you declare a variable but never assign it? In many languages: memory garbage or a crash. In Go — the first safety win: every type has a zero value, and it prints safely, with no error at all.
package main
import "fmt"
func main() {
var count int
var done bool
var note string
fmt.Println("count:", count)
fmt.Println("done:", done)
fmt.Println("note:", note)
}
The result — no crash:
count: 0
done: false
note:
int→0,bool→false,string→""(empty text).- That is why
varwithout a value is perfectly legal code: "declare now, fill in later".
One more detail — blocks. A variable lives only inside the { } block where it was born: something created inside main is invisible elsewhere. For now we have a single block, but next lesson, when functions appear, this rule starts to matter.
Gotcha. An unassigned
stringis""— empty text, not a "broken" variable. Printingnote:produces what looks like a cut-off line, and beginners conclude something is wrong. It isn't! The text is simply zero characters long. You can check with a comparison:note == ""istrue.