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…


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

Related Content

Empty structs

We finally we have enough knowledge for the EBNF format not to seem completely foreign, so let’s jump back and take a look at that, with the examples provided in the spec… Struct types … StructType = "struct" "{" { FieldDecl ";" } "}" . FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] . EmbeddedField = [ "*" ] TypeName [ TypeArgs ] . Tag = string_lit . // An empty struct.

Struct tags

Struct types … A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored. struct { x, y float64 "" // an empty tag string is like an absent tag name string "any string is permitted as a tag" _ [4]byte "ceci n'est pas un champ de structure" } // A struct corresponding to a TimeStamp protocol buffer.

Struct method promotion

Yesterday we saw an example of struct field promotion. But methods (which we haven’t really discussed yet) can also be promoted. Struct types … Given a struct type S and a named type T, promoted methods are included in the method set of the struct as follows: If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.