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.
name := "Ada" n, err := fmt.Println(name)
Try it
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.
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
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 usedstill applies to every new variable.- An untyped constant on the right gets its default type (
int,float64,rune,string,complex128);x := nildoes 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,switchand function-literal blocks you get new variables. - Shadowed
erris the classic bug: the innererris set, the outer one never changes, and the function returnsnil. Theshadowanalyzer and many linters catch it. - Init statements of
if,forandswitchuse:=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 resulterrreuses it. - Mixed assignment: if you need to assign to a field or an outer variable and declare another, declare with
varfirst and use=. - Type control:
x := 5is anint. For another type writevar x int64 = 5orx := int64(5). - Package level needs
var;:=outside a function is a syntax error.
Related
var,const,if- Lessons: Variables, Multiple return values