What Is the Difference Between var and

= in Go

Use var for explicit types or pre-declaration, and := for concise, type-inferred declarations inside functions.

Use var to declare a variable with an explicit type or to declare without initializing, and use := for short variable declarations that infer the type from the initializer. Use := only inside functions and when at least one variable is new to the scope.

var count int // Declares 'count' as int, value is 0
var name = "Go" // Declares 'name' with inferred type string

// Inside a function:
result := 42 // Declares 'result' as int and assigns 42
name, age := "Alice", 30 // Declares two new variables

// Reassigning existing variables:
var x int
x = 10 // Use '=' for reassignment, not ':='