Installing Go And Your Workspace

May 12, 2016


Introduction

If you are new to Linux or the Mac you might find installing Go to be a bit confusing. It was for me when I started learning Go. Go was the reason I stopped using Windows, which I used for 20 years. Even if you’re experienced with these operation systems, setting up Go might seem a bit of a mystery. With this in mind, let’s walk through installing Go.

Downloading Go

Start by navigating to the Go download page: https://golang.org/dl/

Select the binary distribution for your operating system and run the installation program. After you run the installation program, Go will be installed at the following locations:

Mac: /usr/local/go
Windows: c:\go
Linux: $HOME/go

Note: If you download the tar.gz file for Linux, unzip the file in your $HOME directory under a folder named go. This is the expected and default location. Make sure the $HOME/go/bin folder is in your path.

Once Go is installed you should be able to run the go command from a terminal. Start by running the go env command:

$ go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What you should notice is that the $GOPATH variable is empty. We need to fix this, but to fix it we need to understand what a Go workspace is.

Go Workspace

A Go workspace is a physical location on disk where you will load and work with Go code. I recommend that you maintain a single workspace on your machine. It will be simpler and most editor plugins are easier to configure as well.

Note: There are ways with most editors to create different configurations that could be used to point to different workspaces. So it is not impossible to have multiple workspaces, but I find it is better to maintain a single workspace.

Your Go workspace can’t be the same location that Go was installed in. So for Window and Linux users, please stay away from the c:\go and $HOME/go folders respectively. For Mac users, you would really need to go out of your way to use /usr/local/go but stay away from this location as well.

We need to create a new set of folders to represent the Go workspace. There is a special folder that must exist inside your Go workspace named src and is very important. All the code you load and work on must exist inside this src folder.

The src folder represents the start of your Go workspace. Your GOPATH is an environment variable that points to your Go workspace and represents the physical location on disk where the src folder is located, but does not include the src folder.

Mac and Linux Users

Create folders named code/go/src in your $HOME folder:

cd $HOME
mkdir -p ~/code/go/src

Update the .bash_profile file or your shell profile if there is another one you use:

cd $HOME
nano .bash_profile

At the top of the .bash_profile add these environmental variables:

export GOPATH="$HOME/code/go"
export PATH="$PATH:$GOPATH/bin"

Save the file and then source it:

source .bash_profile

Window Users

Create folders named code\go\src in your $HOME folder:

cd c:\Users\<username>
md code

cd code
md go

cd go
md src

Go to your system settings and add the GOPATH variable and update your PATH.

GOPATH="c:\Users\<username>\code\go"
PATH="...;%GOPATH%\bin"

Then close all terminal windows and open a new one.

Validating Go Workspace

Validate that to Go workspace is configured correctly by running the go env command again:

$ go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bill/code/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

If everything is good, the GOPATH variable will no longer be empty. If the GOPATH variable is still empty, try one more time to create a new terminal window. If still no luck, reach out to me over email: bill@ardanlabs.com

At this point Go is installed and you are ready to write some code. To test this do two things. First, go get the Go linter named golint:

Note: You must have the git client installed and installed to work on the command line. If not then go get will not work. https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

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

If everything works you should see no response. What go get does is clone the golint repo and places the source code inside the Go workspace. It also builds the code and installs the golint binary inside the newly created bin folder inside the Go workspace.

cd $GOPATH/src/github.com/golang/lint
ls -la

total 144
drwxr-xr-x  13 bill  staff    442 May 12 21:52 .
drwxr-xr-x   4 bill  staff    136 Nov 16 18:16 ..
drwxr-xr-x  15 bill  staff    510 May 12 21:59 .git
-rw-r--r--   1 bill  staff     96 May 12 21:52 .travis.yml
-rw-r--r--   1 bill  staff    410 Aug  7  2015 CONTRIBUTING.md
-rw-r--r--   1 bill  staff   1479 Aug  7  2015 LICENSE
-rw-r--r--   1 bill  staff   3098 May 12 21:52 README.md
drwxr-xr-x   4 bill  staff    136 May 12 21:52 golint
-rw-r--r--   1 bill  staff  43808 May 12 21:52 lint.go
-rw-r--r--   1 bill  staff    487 May 12 21:52 lint16.go
-rw-r--r--   1 bill  staff   7607 May 12 21:52 lint_test.go
drwxr-xr-x   4 bill  staff    136 Aug  7  2015 misc
drwxr-xr-x  32 bill  staff   1088 May 12 21:52 testdata

Notice that URL path of golint used in the go get call is represented on disk. The source code was loaded inside the Go workspace, based on the configured GOPATH, inside the src folder.

To validate the bin folder was configured properly with your system PATH, try to run the golint command:

$ golint --help
Usage of golint:
	golint [flags] # runs on package in current directory
	golint [flags] package
	golint [flags] directory
	golint [flags] files... # must be a single package
Flags:
  -min_confidence float
    	minimum confidence of a problem to print it (default 0.8)
  -set_exit_status
    	set exit status to 1 if any issues are found

If everything is good you should see that help output.

Editors

There are a lot of options and plugins for many of the popular editors.

Sublime
http://www.sublimetext.com

VIM
http://www.vim.org/download.php

Victor Farazdagi wrote this post on installing Vim for Go:
http://farazdagi.com/blog/2015/vim-as-golang-ide/

Atom
https://atom.io

Joe Fitzgerald wrote this plugin for Atom
https://github.com/joefitzgerald/go-plus

LiteIDE
http://sourceforge.net/projects/liteide/files

Emacs
This is the configuration Guillaume Charmes uses:
https://github.com/creack/dotfiles

Visual Studio Code
https://code.visualstudio.com

Luke Hobah wrote an amazing plugin for VS Code:
https://github.com/Microsoft/vscode-go

Sublime Installation

I use Sublime and every time I try a different editor, I come back to it. I do love the work Luke did on VS Code but I can’t get past all the editor features in Sublime. I will walk you through installing and configuring the GoSublime plugin.

Note: You must use Sublime Text 3. Sublime Text 2 is no longer compatible with the plugin.

Package Control

If you don’t have the Sublime package control manager installed already then navigate to this web page:

https://packagecontrol.io/installation

Copy the code in the grey box for Sublime Text 3 and then switch back to Sublime. Go to the menu and select View and then Show Console:

The console is a very thin text box that will show up at the bottom of the editor. Paste the code into the thin box and hit enter. This will run the code and install Package Control. Once this is done, quit Sublime and restart it.

Note: On the restart you might see an error or message about something missing. If this happens quit Sublime once again and restart it.

Install GoSublime

Hit the < Command >< Shift >< P > keys to bring up the Package Control dialog. Once it shows, type install and you should see something similar:

Select Package Control: Install Package and then type GoSublime. Select GoSublime and let Package Control install the plugin. Once the plugin is installed it is best to restart Sublime again.

After restarting Sublime you should see that GoSublime was installed by looking under Preferences:

To finalize the installation we need to update the Settings - User document. Select that menu option in the last context menu and add the follow JSON document to the empty file.

{
	"env": {"GOPATH":"/Users/<username>/code/go"},
	"on_save":
	[{
	    "cmd": "gs9o_open", "args": {
		    "run": ["clear"],
		    "focus_view": false
	    }
	},
	{
	    "cmd": "gs9o_open", "args": {
		    "run": ["sh", "go build -i"],
		    "focus_view": false
	    }
	},
	{
	    "cmd": "gs9o_open", "args": {
	        "run": ["sh", "go vet"],
	        "focus_view": false
	    }
	},
	{
	    "cmd": "gs9o_open", "args": {
	        "run": ["golint", "."],
	        "focus_view": false
	    }
	}]
}

You can’t use the $HOME environment variable so navigate to the $HOME folder and run the pwd command:

$ pwd
/Users/<username>

Then use that to configure the $GOPATH in the JSON document. Save this document and now we can test that everything works.

Start by adding your Go workspace folder $GOPATH/src to the list of folders:

Once you add the GOPATH folder, navigate to the golint code we downloaded earlier:

Note: I am using a special style for my folder list so you list may not have the same style.

Select the lint.go source code file and hit < Command > < S > to save the file. This will cause Sublime to build, vet and lint the code. You should see the following show up in Sublime.

Conclusion

I hope this installation guide get you up and running quickly. Now that you can start writing code in Go, check out some of these other resources.

Ardan Labs Go Training
https://github.com/ardanlabs/gotraining
https://github.com/ardanlabs/gotraining/tree/master/courses

Offical Go Resources
http://golang.org/
https://github.com/golang/go/wiki
https://plus.google.com/+golang/posts
http://blog.golang.org/

Dave Cheney
http://dave.cheney.net

Video
http://gophervids.appspot.com/
http://www.youtube.com/user/gocoding

Go In Action
If you would like to try out the book, send me an email: bill@ardanlabs.com
https://www.manning.com/books/go-in-action

Slack and Forum Community
https://invite.slack.golangbridge.org
https://forum.golangbridge.org


Go Training

We have taught Go to thousands of developers all around the world since 2014. There is no other company that has been doing it longer and our material has proven to help jump start developers 6 to 12 months ahead of their knowledge of Go. We know what knowledge developers need in order to be productive and efficient when writing software in Go.

Our Go, Web and Data Science classes are perfect for both experienced and beginning engineers. We start every class from the beginning and get very detailed about the internals, mechanics, specification, guidelines, best practices and design philosophies. We cover a lot about "if performance matters" with a focus on mechanical sympathy, data oriented design, decoupling and writing production software.

Learn More

To learn about Corporate training events, options and special pricing please contact:

William Kennedy
ArdanLabs (www.ardanlabs.com)
bill@ardanlabs.com