/home/roman/stdout

Setting up `go get` redirect for your own packages

Dec 17, 2016

I really like the way dependencies are implemented in Go. You can “import” a Git repository and quickly get started. This is done by importing a package like this:

import "github.com/gentlecat/publisher"

You’d download all the packages you need with a go get command.

There might be an issue if you decide to move to another Git hosting or change your GitHub username, for example. All the packages that depend on yours will stop getting updates or even won’t work at all.

One way to solve this is to have a separate domain name for referencing all of your Go packages. You can then redirect requests to a specific repository (on GitHub or somewhere else). This can be done with a <meta> tag that go get command supports.1 Below is example configuration for nginx that will respond to all requests with a <meta> tag that references a repository2 on GitHub.

location ~ ^/([^/]+).*$ {
	if ($args = "go-get=1") {
		add_header Content-Type text/html;
		return 200 '<meta name="go-import" content="$host/$1 git https://github.com/gentlecat/$1.git">';
	}
	return 302 https://github.com/gentlecat/$1;
}

References:


  1. See note at https://golang.org/cmd/go/#hdr-Remote_import_paths about the import path not being a known code hosting site. [return]
  2. One of mine in that case. [return]