Skip to content

Instantly share code, notes, and snippets.

Last active April 28, 2024 21:52
Show Gist options
  • Save rubencaro/5ce32fb30bbfa70e7db6be14cf42a35c to your computer and use it in GitHub Desktop.
Save rubencaro/5ce32fb30bbfa70e7db6be14cf42a35c to your computer and use it in GitHub Desktop.
Golang installation guide

Golang installation guide

Since Golang version 1.11 this process is finally (almost) as easy as it should (!!). You can see full docs here. For older guides see here.

These are my notes, not a generic solution. They are not meant to work anywhere outside my machines. Update version numbers to whatever are the current ones while you do this.

Installing everything needed the first time

Install asdf and its golang plugin, then install golang

asdf lives in

Follow its installation instructions, which at the moment of writing were:

git clone ~/.asdf
cd ~/.asdf
git checkout "$(git describe --abbrev=0 --tags)"

# For Ubuntu or other linux distros
echo '. $HOME/.asdf/' >> ~/.bashrc
echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc

On a new terminal, install Golang plugin:

asdf plugin-add golang

Then install Golang:

asdf install golang 1.15.5

Set some Golang version as global if you don't have none:

asdf global golang 1.15.5

Note on SSH access to github/gitlab remote

If you happen to be using SSH to authenticate with github/gitlab (I recommend that), then you may want to add this to your .gitconfig:

[url ""]
  insteadOf =
[url ""]
  insteadOf =

This will prevent go get from using https login when accessing github.

Routine processes

You should at least read the basics from here.

Setup a new project

You just go into your git root folder, fix your Golang version asdf local golang 1.15.5, and run go mod init public/route/of/your/repo. That will scan all your source files for dependencies, and create a go.mod and a go.sum file. These should go into version control. From now on, any go command you run from within this folder will take those files as reference to lookup their dependencies.

Start working on an existing project

Provided the project was created following this guide, it is straightforward to start working on it. You should clone it, then cd into its folder, run asdf install to get the right Golang version, and build the project, or run its tests. Go will download and build all code depedencies.

For tooling dependencies there should be a script that takes care of running go get for each tool. The actual version installed will be the one in the go.mod file. See Toooling.

Add new dependencies

Simply import the new dependency from a source file, and build your project. Go will catch up with it and update go.mod and go.sum files. If the new dependency was not a Go module already, then it may have some indirect dependencies that also need to be tracked. Just run go mod tidy to track them.

Remove dependencies

Just remove any reference from it on your code, and run go mod tidy. Go will catch up with it and update go.mod and go.sum files.


Tooling is still out of control, as it is not imported (and it should not!) from your code. Thus a simple go build will not install them, even if they are mentioned somewhere inside go.mod. There are some tricks out there that try to help with this though.

Fix tooling versions

The current recommended way to do this (see here and see here) is simply by writing a regular Go code file that imports your tools, thus enabling Go itself to keep track of them as any other dependency. The final trick is to add the +build constraint to avoid being included into regular builds. Some tools.go file like this would do:

// +build tools

package tools

import (
        _ ""

Then run go mod tidy to get it installed and mapped to go.mod.

When you run go get again after you clone this project somewhere else, it will pick up the version fixed in go.mod. It is a good practice to have a script that runs go get for every tool. That way fellow programmers can reproduce the development environment.

Remember to reshim

After installing any golang binary you need to run asdf reshim golang. That will make any binary present in your current Go version to be available from your PATH. That would be the case for a newly installed Go tool, for example.

Copy link

How can I upgrade golang version in asdf?

Copy link

rubencaro commented Feb 16, 2021

@Ppang0405 I don't know your exact use case, and you can do it in many ways.

One way would be just to change the version number by running asdf local golang <version>, then install it asdf install golang <version>. When yo do that then you are running the version you wanted and it's fixed by .tool-versions, so every other dev will use the same version.

You may need to reinstall deps (run since you will be using a different installation of Go.

Copy link

ghost commented Feb 17, 2021

Does anyone have a zsh sample ? Need to convert those functions to zsh.

Copy link

ghost commented Feb 17, 2021

Answering my own question. It seems this is enough for zshrc

function detect_go_workspace_root {
while [[ $path != / ]];
if [ -d "$src" ]; then
echo "$path" && return 0
path="${path:h}" # ignoring symlinks
return -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment