Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The := short variable declaration

Reference for Go's := operator: declaring and initializing variables in one step, redeclaration with at least one new name, shadowing traps and scope.

:= declares new variables and assigns them in a single step, with the types inferred from the right-hand side. It is the everyday way to create local variables in Go. It is only allowed inside functions, never at package level.

The operator has one subtle rule that causes most of its bugs: in a list like a, err := f(), at least one name on the left must be new in the current scope. Names that already exist in that same scope are assigned; names that do not are declared. In an inner scope, every name is new, so outer variables are shadowed.

Go
name := "Ada"
n, err := fmt.Println(name)

Try it

Go Declaring and redeclaring

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go The shadowing trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
x := expr
x, y := expr1, expr2
x, err := f()
Part Description
left side Only plain identifiers (and _). Fields, indexes and pointer dereferences like p.x := 1 are not allowed.
right side One expression per name, or a single call or comma-ok expression yielding several values.

Errors and edge cases:

  • no new variables on left side of := when every name already exists in the current scope. Use = instead.
  • non-name ... on left side of := for anything that is not an identifier.
  • declared and not used still applies to every new variable.
  • An untyped constant on the right gets its default type (int, float64, rune, string, complex128); x := nil does not compile.

Usage notes

  • Same scope, not same function. Redeclaration reuses a variable only if it was declared in the very same block. Inside if, for, switch and function-literal blocks you get new variables.
  • Shadowed err is the classic bug: the inner err is set, the outer one never changes, and the function returns nil. The shadow analyzer and many linters catch it.
  • Init statements of if, for and switch use := too, and the variables live only through that statement and its branches: if v, ok := m[k]; ok { ... }.
  • Function parameters and results count as being in the function's outermost block, so x, err := f() there with an existing named result err reuses it.
  • Mixed assignment: if you need to assign to a field or an outer variable and declare another, declare with var first and use =.
  • Type control: x := 5 is an int. For another type write var x int64 = 5 or x := int64(5).
  • Package level needs var; := outside a function is a syntax error.