Quick Go Setup Guide on Mac OS X

Kyle C. Quest
Aug 9, 2017 · 4 min read

The latest stable version of Go is 1.13.3. The setup instructions below assume you are installing the Go 1.13.3. 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.13.3.darwin-amd64.pkg
shasum -a 256 go.pkg | grep 75f39b2d2b98ab7fe262252cb3f87b14bce71c16df8b9df7ae0fb1db1ba2576c
sudo open 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 introduces support for modules where you no longer have to rely on $GOPATH. It is still an experimental feature though, but hopefully Go 1.14 will make it official and on by default. See the official Go Modules wiki and blog posts to learn more about using modules. If you are starting a new project you should be good to use Go Modules 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 ( $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:

first

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.

golang-learn

All Things Go

Kyle C. Quest

Written by

Cloud/Distributed Computing/Data/All Things Security

golang-learn

All Things Go

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade