:= — short variable declaration: creates and assigns. = — assignment: updates existing.
Short declarations are the idiomatic way to introduce variables in Go functions. Type is inferred. At least one new variable required.
1func main() {2 x := 42 // declares x3 y, z := 1, 2 // declares y, z4 x, w := 3, 4 // OK: w is new5}
Assignment updates existing variables.
1var a, b int2a, b = 1, 2 // assignment3a, b = b, a // swap
When to use:
Common mistakes:
:= when all variables exist.:= at package level.for loops.