Declaring a Variable

The var keyword is the standard way to declare a variable in Go. When you declare a variable without an initial value, it is assigned its “zero value” by default.

Declaration and Assignment

// Declare a variable with its type
// Defaults to the "zero value" (0 for ints)
var myAge int

// Assign a value
myAge = 25

// Declare and initialize in one line
var score int = 100

Zero Values

Go ensures that every variable has a predictable initial state:

  • int: 0
  • bool: false
  • string: "" (empty string)
  • float64: 0.0