Go Vanity URLs

Photo by Andrew Neel on Unsplash

Go Vanity URLs

How to have a custom Golang import domain.

Have you ever seen this type of import?

...
import (
    "go.uber.org/fx"
)
...

Since I came from Java, I always thought Uber had a type of "Maven Central" for Golang.

But that is not the case. And it is cleaner.

What do I need?

One thing: a page with specifics <meta> tags pointing to the actual git repository.

Check this from Uber FX library:

❯ http https://go.uber.org/fx
HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8

<!DOCTYPE html>
<html>
    <head>
        <meta name="go-import" content="go.uber.org/fx git https://github.com/uber-go/fx">
        <meta name="go-source" content="go.uber.org/fx https://github.com/uber-go/fx https://github.com/uber-go/fx/tree/master{/dir} https://github.com/uber-go/fx/tree/master{/dir}/{file}#L{line}">
        <meta http-equiv="refresh" content="0; url=https://pkg.go.dev/go.uber.org/fx">
    </head>
    <body>
        Nothing to see here. Please <a href="https://pkg.go.dev/go.uber.org/fx">move along</a>.
    </body>
</html>

You see that the URL returns a index.html with 3 meta tags pointing to:

  • the git repository

  • the source code inside the repository

  • a redirect to the docs, if you try to access it through the browser

Procedure

So you need to put an index.html page like that one inside your custom domain, and serve it. The go tool will check that page and redirect it to the actual repository.

Example

Using website.org as a custom domain example, If I want the users to use an import like this:

import (
    "go.website.org/mylib"
)

I need to create an index.html like below:

<!DOCTYPE html>
<html>
    <head>
        <meta name="go-import" content="go.website.org/mylib git https://github.com/myusername/mylib">
        <meta name="go-source" content="go.website.org/mylib https://github.com/myusername/mylib https://github.com/myusername/mylib/tree/master{/dir} https://github.com/myusername/mylib/tree/master{/dir}/{file}#L{line}">
        <meta http-equiv="refresh" content="0; url=https://pkg.go.dev/go.website.org/mylib">
    </head>
</html>

And put in a web server for that domain, where the URL

go.website.org/mylib

Serves the index.html above.

And make sure that your repository is publicly available. If not, only you will have access to it.

And more

You can make the root go.website.org as an index list to all your packages like Uber

https://go.uber.org/

To learn more

Check the reference on Golang Docs.

https://pkg.go.dev/cmd/go#hdr-Remote_import_paths

References