Skip to content

Vanity Import Paths

Ondrej Fabry edited this page Jul 17, 2020 · 15 revisions

This document contains information about using vanity import paths for Go packages.

What is Vanity Import Path?

With vanity import paths it will be possible to retrieve Go packages using custom domain. For example:

# instead of this
go get github.com/ligato/vpp-agent

# users can use
go get go.ligato.io/vpp-agent

How to use this?

Setup webserver

To make this work, the webserver running on ligato.io has to serve proper meta tags when the go command client is downloading.

Add redirect

First we setup redirect for /vpp-agent path in the file _redirects:

/vpp-agent* go-get=1 /golang/vpp-agent.html 200

Prepare served page

Then we define the page that will be served to clients:

<html><head>
	<meta name="go-import" content="ligato.io/vpp-agent git https://github.com/ligato/vpp-agent">
	<meta name="go-source" content="ligato.io/vpp-agent     https://github.com/ligato/vpp-agent https://github.com/ligato/vpp-agent/tree/master{/dir} https://github.com/ligato/vpp-agent/blob/master{/dir}/{file}#L{line}">
	<meta http-equiv="refresh" content="0; url=https://godoc.org/ligato.io/vpp-agent" />
</head></html>

Using canonical import path (before Go modules)

⚠️ NOTE: No need to setup canonical import path for projects using Go modules! (go.mod files contains it already)

With canonical import paths we can restrict download of the go packages only to the vanity import path. This will make go command fail when trying to download the package directly from GitHub.

$ go get -v github.com/ligato/infra
github.com/ligato/infra (download)
package github.com/ligato/infra: code in directory /go/src/github.com/ligato/infra expects import "ligato.io/pkg/infra"

To define the canonical import path we add following comment to package declaration:

package main // import "ligato.io/vpp-agent/cmd/vpp-agent"

References