How to Declare Variables in Go

var vs :=

Use var for package-level or typed declarations and := for short, inferred declarations inside functions.

Use var to declare variables at the package level or when you need to specify a type explicitly, and use := for short variable declarations inside functions when the type can be inferred.

// Package-level or explicit type
var count int = 0

// Inside a function, type inferred
func main() {
	name := "Go"
	age := 25
}