Variable declarations

August 11, 2023

I don’t know about you, but I feel like we’ve just spent a month trudging through a dense jungle of generics details… let’s talk about something a bit more applicable to all of our code:

Variable declarations

A variable declaration creates one or more https://go.dev/ref/spec#Variables, binds corresponding identifiers to them, and gives each a type and an initial value.

VarDecl     = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
VarSpec     = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .

Now we’re back in some more familiar territory!

Virtually every language has some form of variable declaration, so this should very familiar to you, even if you’ve never written a line of Go code in your life.

We’re presented with a number of examples to make it all more clear:

var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
var (
	i       int
	u, v, s = 2.0, 3.0, "bar"
)
var re, im = complexSqrt(-1)
var _, found = entries[name]  // map lookup; only interested in "found"

If a list of expressions is given, the variables are initialized with the expressions following the rules for assignment statements. Otherwise, each variable is initialized to its zero value.

This last point is where Go differs from some other languages. You can never have an “uninitialized” variable in Go. Every variable is initialized either to a value you specify, as in:

var x = 3

Or to the zero value for its type:

var x int // x == 0

Quotes from The Go Programming Language Specification Version of August 2, 2023

Share this

Related Content

No short declarations at package level

A couple last points about short variable declarations before we move on… Short variable declarations … Short variable declarations may appear only inside functions. In some contexts such as the initializers for “if”, “for”, or “switch” statements, they can be used to declare local temporary variables. So first: package foo id := 1 // Invalid. Short declarations only possible within fucntions var id = 1 // But this is valid. Don’t ask me why this rule exists.

Short variable declarations, cont'd

Today we’ll look at one of the most interesting, and I would say, confusing, aspects of short variable declarations. Short variable declarations … Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration.

Short variable declarations

Yesterday we saw how to define variables with var, but if you’ve ever actually used Go, you probably know that’s not actually the most common way to define variables. Let’s take a look at the alternative… Short variable declarations A short variable declaration uses the syntax: ShortVarDecl = IdentifierList ":=" ExpressionList . It is shorthand for a regular variable declaration with initializer expressions but no types: "var" IdentifierList "=" ExpressionList .