Quick Go Setup Guide on Mac OS X

The latest stable version of Go is 1.11.1. The setup instructions below assume you are installing the Go 1.11.1. For other versions make sure to update the distribution link and hash information for the version you are trying to install (you can get it from the official download page https://golang.org/dl/ ).

Step 0: Remove the Old Installation of Go (if you have it)

sudo rm -rfv /usr/local/go

Step 1: Download and Install Go

curl -o go.pkg https://dl.google.com/go/go1.11.1.darwin-amd64.pkg
shasum -a 256 go.pkg | grep 5cbd5505288bc2741091561dbfca05f4451824d557e275f373b8449112b84dff
sudo open go.pkg

You can get the link to the current installation package from https://golang.org/dl/ . It’s also where you can get the sha256 checksum for the installer.

Step 2: Setup Your Go Workspace

The default (global) workspace for Go is $HOME/go. If you want to use another location set the $GOPATH variable explicitly in you .profile file (or its equivalent). Go 1.11 introduces support for modules where you no longer have to rely on $GOPATH. It is still an experimental feature though. See the official docs to learn more about using modules.

As a best practice you should also add the bin directory from your workspace to your PATH(in your .profile file).

export PATH=$PATH:$GOPATH/bin

Step 3: Install Additional Dev Tools

go get -u github.com/kardianos/govendor
go get -u github.com/golang/lint/golint

govendor is a dependency management tool you should use to manage your Go application dependencies. It’ll make your life easier. Of course, you can always manage the dependencies manually :-) Another option is to use dep (brew install dep). If you install dep with brew it will also install go and you’ll be using that installation of go unless you make changes to your PATH variable.

golint is one of the static analysis tools you should have. There are many other static analysis tools you can use, but golint with the built-in govet should be in your arsenal from day one. If you are using Github you can also add Go Report Card to your repo to provide additional code quality information.

Step 4: Create Your First Go App

Create the project directory first ( $GOPATH/src/<your_app_location> ):

mkdir -p $GOPATH/src/first
cd $GOPATH/src/first

Add some code (in app.go ):

package main

func main() {
print("my app!\n")

Build and execute your new app to verify that your Go installation is working:

go run app.go

With real apps you’ll need to do a lot more :-)

First, don’t forget about go fmt(to format your source code):

go fmt app.go

Then, run your static analysis tools (at least, golint):

golint app.go

And finally, build your app:

go build app.go

Note that if you don’t specify any filenames the name of the executable will match the name of the project directory. By default, the app executable is located in the same directory.

To build and install the Go app to the bin directory use the install command instead:

go install

Your Go app will be installed to the $GOPATH/bin directory and if you didn’t skip step 2 you will be able to run it like any other apps on your system:


Step 5: Optional IDE Setup

Visual Studio Code is one of the top options for Golang IDEs. Give it a try!

Install Visual Studio Code

Download the current installation package from https://code.visualstudio.com/ .

Install the Go Extension

  • Click on the ‘Extensions’ icon (the last option on the navbar you’ll see on the left side of the IDE).
  • Type ‘Go’ in the search box.
  • Select the extension by Microsoft (used to be by lukehoban). It should be the first option in the search results.

If you want to debug your Go apps in Visual Studio Code you also need to install delve.

If you want an IDE, but you don’t like Visual Studio Code you can try GoLand from JetBrains or vim-go.