Question
In Go, what is the difference between the = and := operators, and when should each one be used?
They both appear to assign values to variables, so it is not immediately clear how they differ in practice.
Short Answer
By the end of this page, you will understand that = is used to assign a new value to an existing variable, while := is used for short variable declaration, which both declares and initializes variables. You will also see where each can be used, how scope affects them, and which beginner mistakes to avoid.
Concept
In Go, = and := are related, but they do different jobs.
=means assign a value to a variable that already exists.:=means declare a new variable and give it an initial value at the same time.
This distinction matters because Go is a statically typed language. Variables must be declared before they can be used. The := syntax is a convenient shortcut that lets Go infer the variable's type from the value on the right side.
= assignment
Use = when the variable has already been declared.
var age int
age = 30
You can also assign a new value later:
age = 31
:= short variable declaration
Use := when you want to create a variable and initialize it in one step.
age := 30
Go infers that age is an int.
Why this matters
This is important in real Go programs because:
- it affects scope
- it affects readability
- it prevents use of undeclared variables
- it helps Go infer types cleanly
A very common source of bugs is accidentally using := inside a smaller scope and creating a new variable instead of updating an existing one.
Mental Model
Think of variables like labeled storage boxes.
:=is like creating a new box, putting a label on it, and placing something inside.=is like opening an existing box and replacing what is inside.
If the box does not exist yet, you cannot use =.
If the box already exists, using := may create a different box in a smaller area instead of reusing the original one.
Syntax and Examples
Core syntax
Declare and initialize with :=
name := "Alice"
count := 10
isAdmin := true
Use this inside functions when you want a quick local variable declaration.
Declare first, assign later with =
var name string
name = "Alice"
Reassign an existing variable with =
score := 100
score = 120
Multiple variables with :=
x, y := 1, 2
Multiple assignment with =
x = 3
y = 4
Or:
Step by Step Execution
Consider this example:
package main
import "fmt"
func main() {
count := 5
fmt.Println(count)
count = 8
fmt.Println(count)
}
Step-by-step
-
count := 5- Go creates a new variable named
count - Go infers its type as
int - The value
5is stored in it
- Go creates a new variable named
-
fmt.Println(count)- The current value of
countis printed - Output:
5
- The current value of
-
count = 8- No new variable is created
- The existing
countvariable is updated to8
-
fmt.Println(count)
Real World Use Cases
Where := is commonly used
Local variables inside functions
userID := 42
name := "Sam"
This is the most common style for short local setup code.
Receiving function results
result, err := doWork()
This is extremely common in Go, especially with error handling.
Loop and temporary values
for i := 0; i < 5; i++ {
fmt.Println(i)
}
Where = is commonly used
Updating state
total := 0
total = total + 5
Assigning after declaration
var configPath string
configPath = "/etc/app.conf"
Reusing existing variables
err
result, err = doWork()
Real Codebase Usage
In real Go codebases, developers usually follow a few common patterns.
1. Prefer := for short local declarations
Inside functions, := is often the default for variables that are first introduced close to where they are used.
file, err := os.Open("data.txt")
This keeps code short and readable.
2. Use = when updating existing variables
count := 0
count = count + 1
This makes it clear that the variable already exists and is being changed.
3. Use var plus = when zero value or wider scope is useful
var err error
var data []byte
data, err = os.ReadFile("config.json")
This is common when a variable must be used across several branches or later in the function.
4. Be careful with error shadowing
A common real-world bug happens when a developer writes := and accidentally creates a new variable in an inner scope.
Common Mistakes
1. Using = before declaring a variable
Broken code:
name = "Alice"
Why it fails:
namedoes not exist yet- Go requires variables to be declared before assignment
Fix:
name := "Alice"
Or:
var name string
name = "Alice"
2. Using := for an existing variable in the same scope
Broken code:
age := 20
age := 21
Why it fails:
- the second line does not introduce any new variable
:=requires at least one new variable on the left side
Fix:
age := 20
age = 21
3. Accidental shadowing inside a block
Comparisons
| Concept | := | = |
|---|---|---|
| Main purpose | Declare and initialize | Assign to an existing variable |
| Creates a new variable | Yes | No |
| Can update existing variable | Only if at least one left-side variable is new | Yes |
| Type inference | Yes | No by itself |
| Allowed at package level | No | Yes, after declaration |
| Common use | Local variables inside functions | Reassignment and updates |
var vs :=
Cheat Sheet
Quick reference
Use := when
- you are declaring a new variable
- you are inside a function
- you want Go to infer the type
name := "Go"
count := 10
result, err := doWork()
Use = when
- the variable already exists
- you want to change its value
- you declared it earlier with
varor:=
count = 20
err = nil
x, y = 1, 2
Rules to remember
:=declares and initializes=only assigns:=works only inside functions:=needs at least one new variable on the left side=cannot be used with undeclared variables:=can accidentally shadow outer variables
FAQ
Can I use := everywhere in Go?
No. := can only be used inside functions. At package level, use var.
Is := just a shorter version of var?
It is a short declaration form for local variables. It both declares the variable and initializes it, with type inference.
Can I use = to create a variable?
No. = only assigns to a variable that already exists.
Why does Go say "no new variables on left side of :="?
Because := requires at least one variable on the left side to be newly declared in the current scope.
What is variable shadowing in Go?
Shadowing happens when := creates a new variable inside an inner scope with the same name as an outer variable.
Should I prefer := or var in Go?
Inside functions, := is often preferred for short local variables. Use var when you need explicit types, zero values, or package-level declarations.
Mini Project
Description
Build a small Go program that tracks a user's score. This project helps you practice when to declare variables with :=, when to update them with =, and how to avoid accidentally creating a second variable in a nested block.
Goal
Create a program that declares a score, updates it several times, and prints the final result correctly.
Requirements
- Declare at least one variable using
:=insidemain - Update an existing variable using
= - Use an
ifblock and avoid accidental shadowing - Print the score before and after updates
Keep learning
Related questions
Check if a Value Exists in a Slice in Go
Learn how to check whether a value exists in a slice in Go, and why Go has no Python-style `in` operator for arrays or slices.
Concatenating Slices in Go with append
Learn how to concatenate two slices in Go using append and the ... operator, with examples, pitfalls, and practical usage.
Convert String to Integer in Go: Idiomatic Parsing with strconv.Atoi
Learn the idiomatic way to convert a string to an int in Go using strconv.Atoi, with examples, errors, and common mistakes.