farthergate.com/posts/2025-02-13-go-module-imports.md
2025-02-14 03:36:11 +00:00

1.2 KiB

title published_on
Custom import paths for Go modules 2025-02-13

Go modules are great. URL-based imports, generally, are great. One of the things that makes them so great is that putting them on your own domain provides a good way of verifying ownership without going through a third party or a complicated review process. Plus, it looks cool.

So, how exactly do you do such a thing? Well, it's buried in the documentation, but it's really pretty simple. For each import path (e.g. mysite.com/mypackage), just put a page on your website with a go-import meta tag:

<meta name="go-import" content="mysite.com/mypackage git https://ultragreatgithost.com/me/mypackage">

(git can be replaced with bzr, fossil, hg, or svn for different VCSs — see the docs page linked above.)

This website, for instance, builds these pages from a hash in build.rb:

def go_modules = {
  "farthergate.com/stack" => "https://git.farthergate.com/aleks/stack",
  # ...
}

See farthergate.com/stack for the generated page. Look in the web inspector for the meta tag!