← Back to Go Course | Chapter 2: Variables & Types | Lesson 2 of 7

Short Variable Declaration with :=

The := shortcut lets you make a labeled box and put something in it at the same time, without spelling out what kind of box it is.

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

markup
package main

import "fmt"

func main() {
	name := "Priya"
	age := 28
	fmt.Println(name, "is", age, "years old")
}

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 :=

markup
package main

import "fmt"

func main() {
	x, y := 10, 20
	sum, diff := x+y, x-y
	fmt.Println("sum:", sum, "diff:", diff)
}

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

markup
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)
}

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

markup
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)
}
Common Mistakes
  1. Trying to use := outside a function body (at package level), where only var is allowed.
  2. 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.
  3. Accidentally shadowing an outer variable by using := inside an if/for block, creating a new local variable instead of reusing the outer one.
Chapter Summary
  • ':=' 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.