Declaring Variables in Go
var, :=, and type inference
What Is a Variable?
A variable is a named storage location for a value. In Go, every variable has a type that is fixed at compile time.
- Go is statically typed — types cannot change at runtime
- Variables always have a default zero value when not explicitly set
- Clear naming matters: Go favors short, clear names over long ones
var Declaration
The var keyword declares one or more variables explicitly:
package main
func main() {
var name string
var age int
var pi float64 = 3.14
_ = name
_ = age
_ = pi
}All lessons in this course
- Declaring Variables in Go
- Go's Built-in Types
- Constants and iota
- Type Conversions in Go