Var vs := in Go: Key Differences and When to Use Each
var declares variables with optional initialization and explicit type, usable anywhere in the code. The := operator is a shorthand for declaring and initializing variables inside functions, inferring type automatically and requiring at least one new variable.Quick Comparison
Here is a quick side-by-side comparison of var and := in Go.
| Feature | var | := Operator |
|---|---|---|
| Declaration location | Anywhere (package level or inside functions) | Only inside functions |
| Type specification | Optional (can specify type explicitly) | Type inferred automatically |
| Initialization | Optional (can declare without value) | Must initialize with a value |
| Re-declaration | Can re-declare variables in different scopes | Requires at least one new variable in the declaration |
| Use case | For package-level variables or explicit typing | For quick local variable declaration and initialization |
Key Differences
The var keyword in Go is used to declare variables either at the package level or inside functions. You can specify the variable's type explicitly or let Go infer it if you provide an initial value. Variables declared with var can be declared without an initial value, in which case they get the zero value of their type.
On the other hand, the := operator is a shorthand syntax used only inside functions to declare and initialize one or more variables at the same time. It always infers the type from the right-hand side values and requires at least one variable on the left side to be new (not previously declared in the same scope).
Another important difference is scope: var can be used for package-level variables, which are accessible throughout the package, while := is limited to local variables inside functions. Also, var allows declaration without initialization, but := requires initialization.
Code Comparison
Using var to declare and initialize variables:
package main import "fmt" var x int = 10 var y = "hello" var z float64 func main() { fmt.Println(x) // 10 fmt.Println(y) // hello fmt.Println(z) // 0 (zero value for float64) }
:= Operator Equivalent
Using := inside a function to declare and initialize variables:
package main import "fmt" func main() { x := 10 y := "hello" z := 0.0 fmt.Println(x) // 10 fmt.Println(y) // hello fmt.Println(z) // 0 }
When to Use Which
Choose var when you need to declare variables at the package level, want to specify the type explicitly, or declare variables without immediate initialization. Use := inside functions for quick, concise declaration and initialization of local variables with inferred types. Remember, := cannot be used outside functions and requires at least one new variable in the declaration.
Key Takeaways
var declares variables anywhere with optional type and initialization.:= is a shorthand for local variable declaration and initialization inside functions only.var can declare without initializing; := always initializes and infers type.:= requires at least one new variable; var can redeclare in different scopes.var for package-level or explicit typing, := for quick local declarations.