Reader Question: Understanding (*T)(nil) syntax

I got a great email from a reader (I didn’t ask their permission to share, so keeping it anonymous) in response to last week’s message Interface variables, pointing out the somewhat essoteric syntax mentioned at the end of the example section: x = v // x has value (*T)(nil) and dynamic type *T The (*T)(nil) syntax is easily confusing. It has been for me, too. So let’s dig into that, and hopefully quell that confusion, once and for all!

Book Reviews

5 min read


Book Review: The Go Programming Language

A great book, but sorely outdated.


Variables and zero values

Variables A variable’s value is retrieved by referring to the variable in an expression; it is the most recent value assigned to the variable. If a variable has not yet been assigned a value, its value is the zero value for its type. The zero value is covered in greater detail near the end of the spec, but it’s an important concept, so let’s discuss it a bit here.


Interface variables

Variables … Variables of interface type also have a distinct dynamic type, which is the (non-interface) type of the value assigned to the variable at run time (unless the value is the predeclared identifier nil, which has no type). The dynamic type may vary during execution but values stored in interface variables are always assignable to the static type of the variable. We haven’t really talked about interfaces yet, as that’s coming up in the next section of the spec about types.

Subscribe to Boldly Go: Daily

Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.

Unsure? Browse the archive.


Variable types

Variables The static type (or just type) of a variable is the type given in its declaration, the type provided in the new call or composite literal, or the type of an element of a structured variable. And here we are at one of the core features of Go, it’s static type system. Each variable has a specific static type, which is typically provided in the variable definition, although there are a few cases where the type can be inferred.


Variables and struct fields

Variables Structured variables of array, slice, and struct types have elements and fields that may be addressed individually. Each such element acts like a variable. This is pretty straight forward. In most contexts, the elements of a struct, array, or slice, can be treated as variables. They can be assigned, or assigned to, using the same syntax. A few examples to illustrate: type Person struct { ID int Name string Age *int } var p Person // p is an instance of the Person struct, with each field initially // set to its zero value.

Book Reviews

9 min read


Book Review: Go Fundamentals

Very thorough, perhaps to a fault, this book is probably a great second read for most Go newcomers.


Variables

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.

Book Reviews

5 min read


Bonus Book Review: Learn Go with Pocket-Sized Projects

A bonus review of this early-access book, which I'm quite excited about.


Constants, typed vs untyped

Constants … Constants may be typed or untyped. Now that’s interesting. And powerful. This is one of the features of Go I featured in my recent video 10 Reasons I Like Go. This feature really helps bridge the gap between the convenience of dynamically typed languages, and the safety of statically typed languages. Let’s look at the details… Literal constants, true, false, iota, and certain constant expressions containing only untyped constant operands are untyped.

Book Reviews

7 min read


Book Review: Go For Beginners

This well-written, quick intro to Go for professional developers has some really strong points, but some important down sides, too.