Variables

February 6, 2023

Last week Go 1.20 was released! It did include a few changes to the Go spec, but none of them affect the portions we’ve covered so far in this series, so we’ll just continue on our merry way…

Variables

A variable is a storage location for holding a value. The set of permissible values is determined by the variable’s type.

A variable declaration or, for function parameters and results, the signature of a function declaration or function literal reserves storage for a named variable. Calling the built-in function new or taking the address of a composite literal allocates storage for a variable at run time. Such an anonymous variable is referred to via a (possibly implicit) pointer indirection.

This is pretty straight forward, and should be a concept familiar to anyone who has ever done any computer programming at all.

A variable is a place in memory to store a value. Ta-da!

The only real nuance here is that variable declarations come in two vareities, those that reserve space for the variable directly (i.e. var x int), and those that reserve space for an anonymous variable, referred to via a pointer (i.e. var x = new(int)).

It’s important that the use of new (or a composite literal) allocates space for the pointed-to value, becuase if you just use var x *int, for example, you end up with space allocated for x, but not for the value it points to. In this case, x would be a valid variable, initialized with the default zero value of nil. And an attempt to dereference it will cause a panic, not an empty/zero int value.

Quotes from The Go Programming Language Specification, Version of January 19, 2023


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Operands

I’ll be live coding again today! I hope you can join me! I’ll be continuing where I left off, working on a new feature for my open-source CouchDB SDK, https://kivik.io/. Join me to see how many mistakes a senior Go dev makes while coding. To kick off our dissection of expressions, we’ll look at the term “operand”. You may recall from your studies of algebra that an operand is the “object upon which an operator acts.


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.