Go vanity imports

2021-02-21
Go Golang

If you have just started learning Go or already developing in Go for quite some time, you may noticed that most of the Go packages are imported directly from their respective VCS repositories, but there are some packages imported from URL’s like golang.org/x/text or go.uber.org/zap . If you try to visit such URL’s, you may see different results like being redirected to documentation or just some dummy page. This is completely different from packages hosted on GitHub, for example github.com/pkg/errors where you can see the real source code. This feature of Go is called vanity imports.

In this article I won’t explain how is this done, but if you are interrested about the details I recommend reading this article from Márk Sági-Kazár. Instead, I want to show how you can achieve this in a very easy way. At least if you have Kubernetes server, which is a prerequisite for the method that will be described.

Some time ago, I decided to release some interesting packages that I wrote, for example go.ectobit.com/act and I wanted to have vanity import for it. There are several available vanity servers, but as I started learning Rust, I decided to write my own while learning. As I am also a big fan of Kubernetes, I made a Helm chart to allow easy installation of the service. The chart is pretty well documented, but I will explain here the installation on the real example, previously mentioned go.ectobit.com/act . Here is the helmfile:

releases:
- name: vanity
  chart: ectobit/vanity
  namespace: repo
  values:
  - config:
      domain: go.ectobit.com
      packages:
        act: https://github.com/ectobit/act
  - ingress:
      enabled: true
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/ssl-redirect: "true"
        nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
        external-dns.alpha.kubernetes.io/hostname: "go.ectobit.com"
        external-dns.alpha.kubernetes.io/ttl: "1800"
      hosts:
      - host: go.ectobit.com
        paths: ["/"]
      tls:
      - secretName: ectobit-com-tls
        hosts:
        - go.ectobit.com

As you can see in the helmfile, there is a value config.domain which you want to replace with your domain and then you can make a map of your packages under config.packages. It is good practice to prefix your domain with go or some other hostname, otherwise you may introduce difficult rewrites on your main web server.

And that’s all, install the chart and your packages will be available to _ge ge_t from a new URL’s.

Upcoming Go's generics

Find out more about upcoming generics in Go
Go Golang generics

A first impression of Rust from the perspective of a Go developer

Rust is very powerful, but let's see how it compares to Go
Go Rust Kubernetes controller secret replicator

Changes in Go implementation of the new protobuf and gRPC

Differences between APIv1 and APIv2 implementation of protocol buffers and gRPC in Golang
Go Golang protobuf gRPC vscode
comments powered by Disqus