Last modified: Jan 22, 2026 By Alexander Williams
Go Variable Declaration: var vs := Explained
Declaring variables is a fundamental task in Go. You have two main tools: the var keyword and the short variable declaration operator :=. Choosing the right one makes your code clearer and more correct.
This guide explains the key differences. We will cover syntax, scope rules, and best practices. You will learn when to use each form effectively.
Understanding the var Keyword
The var keyword declares one or more variables. You can use it at the package or function level. It allows you to declare a variable without an initial value.
When you use var without a value, Go assigns a zero value. The zero value depends on the type. For numbers, it's 0. For strings, it's "". For booleans, it's false. You can learn more about this in our guide on Go Default Values for Uninitialized Variables.
Here is the basic syntax for var.
// Syntax: var identifier type = value
var name string = "Alice"
var age int // Declared without a value, gets zero value (0)
var isReady bool = true
// Declaring multiple variables at once
var x, y int = 10, 20
var (
width int
height int
)
The main advantage of var is flexibility. You can declare variables outside functions. You can also declare them without an immediate value. This is useful when the value is set later in a conditional block.
Understanding the := Short Declaration
The short variable declaration operator := is a convenient shortcut. It declares and initializes a variable in one step. The type is inferred from the value on the right-hand side.
A key restriction is that := can only be used inside functions. You cannot use it at the package level. It is perfect for local variables where the type is obvious.
package main
import "fmt"
func main() {
// Using := for declaration and initialization
name := "Bob" // Type inferred as string
count := 42 // Type inferred as int
ratio := 3.14 // Type inferred as float64
fmt.Println(name, count, ratio)
// You can declare multiple variables in one line
a, b, c := 1, false, "hello"
fmt.Println(a, b, c)
}
Bob 42 3.14
1 false hello
The short form is clean and readable. It reduces boilerplate code. For more on declaring multiple variables, see Declare Multiple Variables in One Line Go.
Key Differences and Rules
Knowing when to use each form is crucial. The choice affects your code's scope and readability.
1. Scope: Package vs. Function
Use var for package-level variables. Use := only inside function bodies.
package main
import "fmt"
// Package-level declaration MUST use 'var'
var globalConfig = "config.json"
func main() {
// Function-level declaration can use ':=' or 'var'
localTemp := 100
var localName string = "Function Scope"
fmt.Println(globalConfig, localTemp, localName)
}
2. Re-declaration Behavior
You can re-declare variables with := under specific conditions. This is useful in error handling. You can re-declare a variable if at least one new variable is introduced in the same scope.
package main
import (
"fmt"
"strconv"
)
func main() {
val, err := strconv.Atoi("100")
fmt.Println(val, err) // First declaration of err
// Re-declaration is allowed because 'newVal' is new
newVal, err := strconv.Atoi("two")
fmt.Println(newVal, err)
}
100 <nil>
0 strconv.Atoi: parsing "two": invalid syntax
You cannot re-declare a variable using var in the same block. This will cause a compile-time error.
3. Explicit Type vs. Type Inference
Use var when you need to specify a type explicitly. This is helpful when the initial value is not the final type you want. For example, you might declare a float64 but assign an int later. For such conversions, check Go Type Conversion: Safe Variable Changes.
Use := when the type is obvious from the right-hand side. This keeps the code concise.
func example() {
// Explicit type with var
var preciseNumber float64 = 7 // Value 7 is an int, but variable is float64
// Type inference with :=
inferredNumber := 7 // Type is int
}
Best Practices and When to Use Each
Follow these guidelines to write clean Go code.
Use `var` when:
- Declaring package-level variables.
- You need to declare a variable without an initial value.
- You want to be explicit about the variable type for clarity.
- Declaring variables in a block with
var ( ... )for grouping.
Use `:=` when:
- Declaring and initializing a variable inside a function.
- The type is obvious from the initial value (e.g.,
name := "Go"). - You need to handle multiple return values, like from a function call.
- You want concise code for local, temporary variables.
A common beginner mistake is trying to use := outside a function. Remember this simple rule. For more foundational rules, see Go Variable Naming Rules for Beginners.
Conclusion
Both var and := are essential in Go. The var keyword is versatile for package-level and explicit declarations. The short declaration operator := is concise for local variables inside functions.
Choose var for package scope and delayed initialization. Choose := for quick, type-inferred declarations within functions. Understanding these tools helps you write idiomatic and effective Go code.
Start by applying these rules in your next project. Your code will be cleaner and easier to maintain.