Vanity Imports in Golang
25 December, 2019
The imports in Golang are the full path of the package including the domain name. It is common to host the packages on Github and use it for import as well. However, there are two problems with it.
- Long imports: Packages under Github have to have
github.com/<username>/<repository>at the beginning which is rather long considering some repositories have nested sub-directories. This is true for internal packages in the repository as well, when you want to import a package from a different sub-directory but you still have to use the entire Github prefix.
- Hassle in switching the hosting The package name is the unique identifier for the package and having the hosting provider name in it means its a hassle to switch provider even for internal packages. The two aforementioned problems can be solved using vanity imports in Go. What vanity import means is using a custom domain for package imports.
github.com/<username>/<repository>/pkg/<xyz> -> <user.tld>/xyz
There are few ways to do it, but all essentially boil down to adding meta tags in the response which go command can parse to figure out where is the actual code located. I am thinking of building a system to easily generate static files as a weekend project.