Setting up my environment for Golang

This article was published on Apr 19, 2022, and takes approximately 2 minutes to read.

The first thing to get started with any programming language is setting up the environment.

In JS we could use the browser (to learn at least), but it seems we don't have this luxury in Go (not as far I as known)

Installation

Because I use ASDF to manage runtimes, I don't go directly to the Go website and download it. Instead, I always try to google for "<programming-language> asdf" and find a repository that provides a plugin to ASDF so I can install it from there. In this case, asdf-golang.

I'm currently using a MacBook PRO with M1 PRO Processor, so things might be different for you.

First, I have to ensure I have all coreutils needed by the plugin:

Copied
brew install coreutils

Then, we need to install the asdf plugin itself:

Copied
asdf plugin-add golang https://github.com/kennyp/asdf-golang.git

With this installed, we finally can get the Go runtime. The only caveat for me is that because I'm using the Apple's processor, the regular instruction doesn't work by default. I need to pass a environment variable to get it to the correct architecture:

Copied
ASDF_GOLANG_OVERWRITE_ARCH=amd64 asdf install golang lastest

At this point, the latest is pointed to Go@1.18.

Last, I have to inform asdf the version of Go I want to be available globally. That's handy for projects that use a specific runtime version vs. the one you often use:

Copied
asdf global golang latest

Now, when I run `go version` I get:

Copied
➜  ~ go version
go version go1.18 darwin/amd64

Go Workspaces

I'm still discovering this topic, but it seems Go defines where the code will be installed and where it'll get modules from.

When installed, it expects us to create a folder at the user root level called go, and inside it, having three folders:

  • pkg -> contains all shared packages;
  • src -> where we're going to add all projects and source code from all projects (that's a bit weird, I know it can be more flexible than that);
  • bin -> where the binaries will live

So, let's create them:

Copied
# This will create and native to a folder called "go" inside my root folder user (~), then, creating it inside 
take ~/go && mkdir src bin pkg

VSCode

Go has an effective integration with VSCode. They offer an official extension that handles everything you need for the text editor.

You can download the Go extension here.

Once you install it, it'll ask you permission to install many other packages to provide all the goodies.

Resources