Google has announced Go v1.9!


Google announced the release of Go 1.9 today. There are many new features: compiler improvements, a math/bits package for bit-twiddling, a monotonically-increasing clock type—but the most interesting language-level feature introduced is the addition of type aliases.

A type alias is, well, what it sounds like: it introduces a new type-level identifier that is functionally identical to the another type. (They are similar to typedefs in C or type synonyms in Haskell.) Type aliases are introduced with a new syntactic construction:

type NewAlias = SomeExistingType

This stands in contrast to Go’s standard type declarations, which introduces a new, distinct type. For example, with type aliases, we can declare epoch as an alias for an unsigned 64-bit integer, and assign it an explicit uint64 value:

type Epoch = uint64
var beginningOfTime Epoch = uint64(0)

However, if we used a standard type declaration, the above code wouldn’t work, as Epoch is a distinct type, to which values of type uint64 cannot be bound without an explicit cast:

type Epoch uint64
var beginningOfTime Epoch = uint64(0) // this doesn't compile!

Of course, if you add an explicit cast, everything’s fine and dandy:

var justAfterThat Epoch = Epoch(1)

Before Go 1.9, the language specification provided two type aliases: byte can be used in place of uint8, and rune (which is used to represent character literals) can be used in place of uint32. The addition of user-defined type aliases allows you, the programmer, to access this feature that was reserved for the language implementors.

But, the question: given that type definitions are more powerful and more type-safe than aliases, why would you want to use aliases? The usual answer—and the one that Google cited in their blog post—is to aid in refactoring. When faced with a large and unfamiliar codebase, it may not be feasible to add new type declarations for each separate domain concept. Type aliases can provide a measure of clarity to a codebase without requiring extensive re plumbing and addition of casts.

While the compiler doesn’t yet support type aliases, it’s been added to our list of important features.