When to use var instead of := in Go
Fast recall
Quick revision
Core answer
var when declaring a package-level variable, when you want a variable to begin with its zero value, or when an explicit type makes the code clearer. Use := for short local declarations inside a function when the initial value already makes the type obvious. Both create typed variables; the important differences are scope, initialization, and readability—not runtime speed.Whether you understand where each declaration form is legal and how initialization differs.
Using `:=` at package scope or using it when every name on the left already exists in the same scope.
Explain zero values and the rule that a short declaration must introduce at least one new name.
Interview-ready guide
Interview answer
varand:=both create statically typed variables, but they serve different declaration contexts.varis the complete declaration form and works at package scope or inside a function.:=is short-declaration syntax available only inside a function body.I use
varwhen a package-level variable is required, because:=is not legal there. I also use it when the zero value is meaningful. For example,var retries intintentionally starts at0, andvar buffer bytes.Buffercreates a usable empty buffer without a separate constructor.An explicit type can make
varclearer even when an initial value exists.var reader io.Reader = filetells the reader that the code relies on theio.Readercontract, whereasreader := filegives the variable the concrete file type. The explicit form can therefore communicate an abstraction choice.I use
:=for short local declarations when the right-hand side already explains the type.name := "Ada"is easier to read than repeatingstring, anduser, err := repository.Find(id)follows the common Go pattern of receiving a value and an error together.For example, a function might begin with
var result Resultbecause several branches assign it, then usevalue, err := parse(input)for new local results. Later updates useresult = valueorvalue, err = parse(other)because those names already exist.A short declaration must introduce at least one new name in the current scope.
retries, city := 2, "Pune"is valid whenretriesexists andcityis new, butretries, city := 3, "Delhi"in the same scope is not a declaration and must use=.I also consider scope carefully because
:=inside a nested block can shadow an outer variable. My default is concise:=for obvious local values, but I choosevarwhenever scope, zero-value initialization, or an explicit type makes the intention clearer. There is no runtime speed difference driving this choice.
When to use var instead of := in Go
Direct Answer
Use var when declaring a package-level variable, when you want a variable to begin with its zero value, or when an explicit type makes the code clearer. Use := for short local declarations inside a function when the initial value already makes the type obvious. Both create typed variables; the important differences are scope, initialization, and readability—not runtime speed.
Code Example
package main
import "fmt"
var appName = "InterviewExplainer" // package scope: var is required
func main() {
var retries int // int zero value: 0
name := "Ada" // new local string
retries, city := 2, "Pune" // retries exists; city is new
name = "Grace" // assignment, not declaration
fmt.Println(appName, retries, name, city)
}
State after each important line:
| Line | Variable state | Why |
|---|---|---|
var retries int | retries = 0 | int receives its zero value |
name := "Ada" | name = "Ada" | new local name; type inferred |
retries, city := 2, "Pune" | retries = 2, city = "Pune" | valid because city is new |
name = "Grace" | name = "Grace" | existing variable is reassigned with = |
Common Mistakes & Pitfalls
1. Short declaration at package scope
package main
name := "Ada" // compile error: non-declaration statement outside function body
Use var name = "Ada" instead.
2. No new variable on the left
func main() {
count := 1
count := 2 // compile error: no new variables on left side of :=
}
The second line is assignment, so write count = 2.
Check your understanding
Answer these without looking back at the article.
- Why can `retries, err := strconv.Atoi(value)` be valid when `retries` already exists?
- How can `:=` accidentally shadow a variable from an outer scope?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.