Go Reference ยท Keywords and Statements
The var declaration
Reference for Go var: declaring variables with types, initializers and groups, zero values for every type, package-level versus local scope and common errors.
var declares one or more variables. Every variable has a type, either written out or inferred from its initial value, and every variable always has a value: if you provide none, Go sets it to the zero value of its type. There is no such thing as an uninitialized variable in Go.
var works at package level and inside functions. Inside functions you will often use the shorter := instead, but var is the only form allowed at package level and the clearest choice when you want the zero value.
var count int var name = "gopher" var x, y float64 = 1.5, 2.5
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
var Name Type var Name = value var Name Type = value var Name1, Name2 Type = v1, v2 var ( Name1 Type Name2 = value )
| Part | Description |
|---|---|
Name |
One or more identifiers separated by commas. |
Type |
Optional if an initializer is given. If both are present, the value must be assignable to the type. |
value |
Optional if a type is given. A list must have one expression per name, or a single call returning that many values. |
Zero values:
| Type | Zero value |
|---|---|
| numbers | 0 |
bool |
false |
string |
"" |
| pointer, function, interface, slice, map, channel | nil |
| array, struct | every element or field set to its own zero value |
Errors and edge cases:
- A local variable that is declared and never used is a compile error:
declared and not used. Package-level variables are exempt. - Declaring the same name twice in one scope is
redeclared in this block. - With no type and an untyped constant initializer, the default type applies:
int,float64,rune,string,boolorcomplex128. var x = nilis an error:nilhas no type to infer.
Usage notes
- Package-level initialization order follows dependencies, not source order. Go analyzes which variable uses which and initializes them in a valid order; a cycle is a compile error.
- Scope: a variable is visible from the end of its declaration to the end of its block. Inner declarations of the same name shadow outer ones.
- Prefer the zero value when it is useful:
var sb strings.Builder,var mu sync.Mutexandvar wg sync.WaitGroupare ready to use. nilslices behave like empty ones forlen,appendandrange, butnilmaps cannot be written to.- Since Go 1.22 each iteration of a
forloop gets its own copy of loop variables declared in the loop header, so closures capture per-iteration values. - Use
varfor the zero value or for an explicit type that differs from the default (var timeout time.Duration = 5), and:=for everything else inside functions.