Quick Go Setup Guide on Mac OS X

Kyle C. Quest (Q)
golang-learn
Published in
4 min readAug 9, 2017

The setup instructions below assume you are installing the Go 1.16.2. 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

#download the installer package
curl -L -o go.pkg https://golang.org/dl/go1.16.2.darwin-amd64.pkg
#verify the package checksum to make sure you downloaded the right thing
shasum -a 256 go.pkg | grep c3a2e19e49041d835c18a055f34fda1ea56a8599ad12f03a1bfe88d7608f3f5e
#run the installer
sudo open go.pkg
#remove the installer package
rm go.pkg

If the second command doesn’t print the sha256 string back then something is not right :-)

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 introduced the initial support for modules where you no longer have to rely on $GOPATH. With Go 1.14 Go modules are finally ready for production and they are no longer an experimental feature. See the official Go Modules wiki and blog posts to learn more about using modules. Use Go Modules unless you have a specific reason not to use them and if you do then you don’t need to worry about $GOPATH and where you put your project.

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

If you are using Go Modules for your new project you don’t need to install any additional dependency management tools. If you still want to use one of the 3rd party tools like dep then you need to install it too.

Regardless of the dependency management tool you use it’s a good idea to use vendoring and commit your vendor directory with your application project repository to guarantee that your application builds and you have the dependencies you expect, so if the network is down or if the dependency owner decides to make unexpected/breaking changes to the libraries your application is still ok. With Go 1.13 you also get the module proxy feature (using as the https://proxy.golang.org module proxy server by default), which reduces the need to vendor your dependencies in some cases.

Note that it’s not recommended to commit the dependencies if you are creating a library.

go get -u golang.org/x/lint/golint

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 Go Modules and you want to install golint from the project directory make sure to add GO111MODULE=off in front of the command to install it otherwise golint will be added to our project’s go.mod file and that’s probably not what you want :-)

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 ( anywhere/<your_app_location> ):

mkdir -p anywhere/first
cd anywhere/first
go mod init my.app

Running go mod will produce go.mod for your project. Note that the module name/path must have a dot in it (you don’t have to worry about it if your module name has a Github path in it, e.g., github.com/your_github_username/your_project).

Add some code (in app.go ):

package mainimport "fmt"func main() {
fmt.Println("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.

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.

Next Steps

Make sure you go through the interactive Tour of Goto learn about the language and how to use it and then at a look at the 50 Shades of Go blog post to learn about the gotchas you might encounter. Once you are comfortable enough experimenting with toy projects take a look at the Go Project Layout post to get ideas about how to structure your Go projects.

--

--

Kyle C. Quest (Q)
golang-learn

CTO / Redefining DevOps * Hacker @DockerSlim * @Golang50Shades * Cloud Native * Data * Security