Image by Kelvyn Skee

Canonical import paths in Golang

One of the features that has contributed to Go’s steady growth is the extremely friendly and minimalistic approach to package management via “go get”. Extreme simplicity however, does not come without limitations. Amongst other problems, having URLs as import paths makes your imported packages subject to link rot.

The upcoming shutdown of google code for example, will soon render hundreds of import paths obsolete. You should, of course, vendor your external dependences, and the Go team is currently working on a standard method to do so. But when it comes to the code that you expose to the world, there is a way for you to make it resilient to repository changes, define a canonical import path.

The go tool does not only fetch code from known source code hosting platforms, it does more, as you can read in the documentation about remote import paths:

If the import path is not a known code hosting site and also lacks a version control qualifier, the go tool attempts to fetch the import over https/http and looks for a tag in the document’s HTML .

The meta tag has the form: <meta name="go-import" content="import-prefix vcs repo-root">

This means that if you have an html page under with the go-import meta-tag pointing to the repo, you can then use as your import path, and the go tool will work with it seamlessly as if it was hosted under that URL, subpackages included. If some day you decide to move your code to you simply need to change your meta-tag and nobody will need to touch their code. Thus works as a permanent alias for your repo, a custom import path.

The Go Team itself has started using this technique. When I wrote my previous post about gorename, the import path was under Since then the code has been moved and now, along with all other go tools, it lives under the alias You can check for yourself how they do it:

➜ curl
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content=" git">
<meta name="go-source" content="{/dir}{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=">
Nothing to see here; <a href="">move along</a>.

As you can see, is just an alias for the git repository The other meta-tag go-source is indicating the go tool to generate the documentation links from the github mirror repo.

This is very useful to prevent link rot, but you might have noticed that it opens the door for some new problems. In one hand, if someone just finds your library in github, it’s unlikely that they’ll try to find a better import path to use instead. And what happens if we import something in 2 different places, one via the repository URL and another via its custom URL? Since the import path is the unique identifier for a package and the Go compiler creates an statically linked binary, we’ll end up with 2 copies of the same code in our executable.

To avoid this situation, Go 1.4 introduced the concept of canonical import paths:

Go 1.4 introduces an annotation for package clauses in Go source that identify a canonical import path for the package. If an import is attempted using a path that is not canonical, the go command will refuse to compile the importing package.

The syntax is simple: put an identifying comment on the package line. For our example, the package clause would read:

package pdf // import ""

You can see this at work with gorename’s example. The canonical import path defined is And if when trying to “go get” from the github repository the go tool will let you know:

➜ go get
can't load package: package code in directory /tmp/gopath/src/ expects import ""
[ golang ] [ dev ]

Who's this?

This is the blogging attempt of a middle-aged southern european IT guy working in northern europe.

Most recent posts

Other projects