go get With Style — or — Take Back Your Go Import Paths!

go get sure is neat, huh? Want some code, and know that it's hosted on Github? just go get github.com/USER/REPO and you're good to go! But what if you're a go-getter (heh) and want some vanity in your life?

How about this?

$ go get jameshunt.us/bolo/core

It's incredibly easy to do. You'll need the following:

  1. An SSL/TLS certificate (check out these guys)
  2. The ability to modify the raw HTML of your website
  3. Some nginx-fu (other web servers are do-able, but won't be covered here)

Set Your Meta

Let's say you want to be able to do this:

$ go get example.com/extras/fun

yet pull the code from the example/extras-fun repo on Gihub?

All you need to do is add the following tag to the page at http://example.com/extrafun:

<meta name="go-import"
   content="example.com/extras/fun git https://github.com/example/extras-fun">

That's it.

The content of the meta tag (the metadata itself), is three space-separated tokens:

  1. import prefix ("example.com/extras/fun")
  2. vcs ("git")
  3. repository ("https://github.com/example/extras-fun"

The import prefix is the top-level of the module being imported. More on that lter.

The vcs is a short string identifying which version control system needs to be used to pull down the repository code. Valid values are "git", and some others that I don't use.

The repository is the full URL to the repository to retrieve. In other words, our Github repository URL (in read-only mode).

What About Submodules?

If you have submodules (say extra/fun/times) that need to be go-gettable in their own right, you do the same thing. I mean, you add the exact same meta tag to the page for times:

<meta name="go-import"
   content="example.com/extras/fun git https://github.com/example/extras-fun">

You have to do the first step though. Why? Given:

$ go get example.com/extras/fun/times

go get will do the following:

  1. Retrieve https://example.com/extras/fun/times
  2. Look for the go-import tag
  3. Check that the import prefix matches "example.com/extras/fun/times"
  4. Seeing that it does not:
    1. Retrieve https://example.com/extras/fun
    2. Look for the go-import tag
    3. Check that the import prefix matches "example.com/extras/fun"
    4. Clone the $vcs repository at $repository

Note that step 4.c does its matching based on the previous import prefix (as determined in step 3)

Wait, Why SSL?

At the outset, I mentioned needing an SSL/TLS certificate. Technically speaking, you don't need it, but if you don't encrypt your endpoint, go-getters will need the ugly -insecure flag. It's best just to avoid it, given that you can get a single-domain certificate for about $5/year from here.

James (@iamjameshunt) works on the Internet, spends his weekends developing new and interesting bits of software and his nights trying to make sense of research papers.

Currently working on Rook.