Go: Aliases, Simple and Efficient

Vincent
A Journey With Go
Published in
3 min readApr 13, 2020

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ This article is based on Go 1.13.

The aliases have been brought by Go 1.9 and allow developers to provide an alternate name for an existing type. This feature aims to facilitate the refactoring of large codebases, making it crucial for large projects.

Aliases have been implemented after several months of consideration on the best way to bring aliases to Go language. A first proposal with general aliasing (type, function, etc.) has been replaced by a simpler form of alias, with a focus on the types, the main need in this feature. Focusing on aliasing types makes the implementation way simpler by answering the initial problem. Let’s see the solution in action.

Refactoring

The main purpose of introducing aliases was to make the refactoring of large codebases easier. Defining an alias from the old name to the new name allows the developers not to break any compatibility with existing clients. Here is an example with docker/cli:

package command// Deprecated: Use github.com/docker/cli/cli/streams.In instead
type InStream = streams.In

// Deprecated: Use github.com/docker/cli/cli/streams.Out instead
type OutStream = streams.Out

Existing code that was using command.InStream do not break and now use the new type streams.In.

--

--