Short Variable Declaration with :=
In this page:
The := Operator
Inside a function, ':=' declares a new variable and infers its type from the value on the right, without needing the var keyword or an explicit type. It's shorthand for 'var x = value' and is by far the most common way Go code declares local variables.
Note: := is the idiomatic way to declare local variables in Go; reach for var mainly when you need an explicit type or a zero value.
Example: The := Operator
package main
import "fmt"
func main() {
name := "Priya"
age := 28
fmt.Println(name, "is", age, "years old")
}
Login to try C/C++/Java/PHP code in the editor
Multiple Assignment with :=
You can declare several variables at once on a single line with ':=', separating names and values with commas. This is especially common with functions that return multiple values, like map lookups or error-returning calls.
Example: Multiple Assignment with :=
package main
import "fmt"
func main() {
x, y := 10, 20
sum, diff := x+y, x-y
fmt.Println("sum:", sum, "diff:", diff)
}
Login to try C/C++/Java/PHP code in the editor
At Least One New Variable Rule
Go allows ':=' to reuse an existing variable in the same scope as long as at least one variable on the left is genuinely new -- the existing one is simply reassigned, not redeclared. If every name already exists, the compiler rejects it with 'no new variables on left side of :='.
Example: At Least One New Variable Rule
package main
import "fmt"
func main() {
count, err := 5, error(nil)
fmt.Println(count, err)
count, ok := 10, true // count reused, ok is new
fmt.Println(count, ok)
}
Login to try C/C++/Java/PHP code in the editor
Shadowing in Nested Blocks
Because ':=' can declare a new variable, using it inside a nested block like an if statement can accidentally create a second, unrelated variable with the same name that shadows the outer one for the rest of that block. This is a common source of subtle bugs for beginners.
Note: If a variable behaves unexpectedly inside an if/for block, check whether ':=' accidentally created a new shadowed variable instead of reusing the outer one.
Example: Shadowing in Nested Blocks
package main
import "fmt"
func main() {
value := 1
if true {
value := 2 // shadows outer value inside this block
fmt.Println("inner:", value)
}
fmt.Println("outer:", value)
}
Login to try C/C++/Java/PHP code in the editor
- Trying to use := outside a function body (at package level), where only var is allowed.
- Using := again on a variable that already exists in the same scope when no new variable is being introduced, causing a 'no new variables' compile error.
- Accidentally shadowing an outer variable by using := inside an if/for block, creating a new local variable instead of reusing the outer one.
- ':=' declares and initializes a variable in one step, inferring its type from the value.
- It can only be used inside function bodies, never at package level.
- At least one variable on the left side of ':=' must be new in that scope.
- Overusing := across nested blocks can accidentally shadow variables from an outer scope.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: