Short Variable Declaration with :=
In this page:
name := value
name1, name2 := value1, value2
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.
Example: The := Operator
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create name and infer the type
name := "Priya"
// Short declaration: create age and infer the type
age := 28
// Print the values followed by a newline
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 :=
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create x, y and infer the type
x, y := 10, 20
// Short declaration: create sum, diff and infer the type
sum, diff := x+y, x-y
// Print the values followed by a newline
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.
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: