var vs := in Go
Fast recall
Quick revision
Core answer
Whether you know scope and type inference rules.
Using := at package scope or assuming it redeclares every name.
Mention that at least one name on the left must be new for :=, while an existing variable may be reassigned in the same scope.
Interview-ready guide
Interview answer
Go provides
varand:=because variable declarations occur in different contexts.varis the general declaration form and works both at package scope and inside a function.:=is short-declaration syntax and works only inside a function body.A
vardeclaration can give the type without giving a value. For example,var retries intcreates anintwith its zero value, which is0. It can also include a value, as invar name = "Ada", or make the intended type explicit, as invar timeout time.Duration = 5 * time.Second.A short declaration combines declaration, type inference, and assignment. In
name := "Ada", Go creates a new local variable and infersstringfrom the value. It is concise when the value already makes the type obvious, which is why it appears frequently in normal function code.The assignment operator
=is different from both forms because it changes a variable that already exists. Aftercount := 1, the next update iscount = 2, notcount := 2, because the second statement introduces no new name.Short declarations have one useful mixed case. If
erralready exists,value, err := load()is valid whenvalueis new in the same scope. The statement declaresvalueand assigns the new result toerr; at least one name on the left must be new.I normally use
:=for clear local initialization andvarfor package variables, deliberate zero values, or cases where an explicit type communicates intent. This is a readability and scope decision, not a runtime performance decision.I also watch for accidental shadowing. Writing
result, err := work()inside a nested block can create a newerrthat hides the outer one. When the value must survive outside that block, I declare it in the correct scope and use=for the update.
var vs := in Go
Direct Answer
var declares a variable and may use an explicit type; := infers the type and is allowed only inside functions. Use := for local values when the type is obvious, and var when you need package scope, a zero value, or an explicit type.
Deep Dive & Architecture
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.
var declares a variable and may use an explicit type; := infers the type and is allowed only inside functions. Use := for local values when the type is obvious, and var when you need package scope, a zero value, or an explicit type.
A common mistake is using := at package scope or assuming it redeclares every name.
Code Example
var retries int
name := "Ada"
retries, name = 2, "Grace"
Common Mistakes & Pitfalls
Using := at package scope or assuming it redeclares every name.
Check your understanding
Answer these without looking back at the article.
- When would you use Variables And Short Declaration in a real project?
- When should you use var instead of := in Go?
- What is a common mistake when using Variables And Short Declaration?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.