Learn to identify and fix the most frequent mistakes in Golang.
:=
and =
in Go
In Go, :=
and =
are both assignment operators,
but they serve different purposes and are used in different contexts.
Letβs break down the differences with examples.
:=
β Short Variable Declaration (Only Inside Functions)The := operator is used to declare and initialize a new variable in a function in a single step. It is a shorthand for declaring a variable and assigning it a value at the same time.
package main
import "fmt"
func main() {
name := "Alice" // Declares and initializes 'name' (string)
age := 25 // Declares and initializes 'age' (int)
fmt.Println(name, age) // Output: Alice 25
}
Here, name
and age
are being declared and initialized at the same time.
The type is inferred automatically (name
is a string, age
is an int).
:=
can only be used inside functions (it does not work at the package level).
=
β Assignment (Reassigning an Existing Variable)
The =
operator is used to assign a new value to an already declared variable.
package main
import "fmt"
func main() {
var age int // Declaring 'age' (default value 0)
age = 30 // Assigning a value to 'age'
fmt.Println(age) // Output: 30
}
var age int
declares the variable but does not initialize it with a value.
age = 30
assigns a new value to the already declared variable.
:=
and =
Feature | := (Short Declaration) |
= (Assignment) |
---|---|---|
Usage | Declare and initialize a variable in one step | Assign a value to an existing variable |
Scope | Only inside functions | Can be used inside functions and at the package level |
Reusability | Cannot be used to reassign a value | Can be used for reassignment |
Type Inference | Yes (Go detects the type automatically) | No (Variable must be declared first) |
:=
When Reassigning a VariableSince :=
always declares a new variable, it cannot be used to reassign an existing one.
package main
func main() {
count := 10 // Declares 'count'
count := 20 // β Error: No new variables on left side of :=
}
package main
import "fmt"
func main() {
count := 10 // Declare and initialize
count = 20 // β
Assign a new value (using =)
fmt.Println(count) // Output: 20
}
You can use :=
in a situation where at least one new variable is being declared while others are being reassigned.
package main
import "fmt"
func main() {
x := 5
y := 10
x, z := 20, 30 // β
x is reassigned, z is newly declared
fmt.Println(x, y, z) // Output: 20 10 30
}
x is reassigned (=
would normally be used).
z is newly declared (:=
is required).
This trick is useful when you want to update one variable and declare another at the same time.
β
Use :=
when declaring and initializing a new variable inside a function.
β
Use =
when you need to update an existing variable.
β
Use var x type
+ `=` when you need a package-level variable or want to declare a variable before assigning a value.
package main
import "fmt"
var globalVar int // Declared at package level (must use `var`)
func main() {
localVar := 100 // β
New variable with `:=`
fmt.Println(localVar)
localVar = 200 // β
Updating existing variable with `=`
fmt.Println(localVar)
globalVar = 500 // β
Assigning value to global variable
fmt.Println(globalVar)
}
:=
is for declaring and initializing variables inside functions.
=
is for reassigning values to existing variables.
Mixing :=
with =
is allowed only when at least one variable is new.
By understanding these differences, you can avoid common bugs and write cleaner Go code! π
π Thank you for reading these articles! If you find this content valuable and want to support my work, a coffee would be greatly appreciated! βπ
π» I am a freelance web developer, and I personally create and maintain this website. Any support would help me improve and expand it further! π