Download Source

Go Setup - Mac/Linux

Overview

This course will show you how to set up a Mac/Linux machine for modern software development with Go.

Core Steps:

  • Install Go
  • Set up environment variables such as GOPATH and adding BIN to your existing PATH

Optional Steps:

  • Install and configure Revision Control Systems
  • Install and configure popular code editors

Installing GO

Preferred Method

Download the latest installer from go: http://golang.org/dl/

Mac - Using Home Brew

It's preferred that you download and install the package. However, if you are a homebrew user, you can use the following commands:

$ brew update
$ brew install go

Linux - Using APT

$ sudo apt-get update
$ sudo apt-get install golang

Note: This can leave you with an old version of go as they don't update the package manager as timely as they should.

The $GOPATH

The $GOPATH is where all Go files must live.

Go 1.8

In Go 1.8, the $GOPATH defaults to $HOME/go if not set explicitly. All earlier versions of Go require this environment variable to be set.

ALWAYS EXPLICITLY SET THE GOPATH

Many tools, especially third party tooling, still depend on an explicit $GOPATH being set.

Setting Up The $GOPATH

Unix/Mac OS

In your .bash_profile, or equivalent file:

export GOPATH="/path/set/here"

$GOPATH Is Not $GOROOT

While you don't need to specifically setup the $GOROOT variable anymore, you might still see it referenced in older materials. This is the path of your GO installation, not your source code.

The $GOROOT is usually something like /usr/local/go. Your $GOPATH is usually something like $HOME/go.

The Go binary distributions assume they will be installed in /usr/local/go, but it is possible to install the Go tools to a different location. In this case you must set the GOROOT environment variable to point to the directory in which it was installed.

Add Go Bin Directory To $PATH

For convenience, add the workspace's bin subdirectory to your PATH:

NOTE: If you used an installer, you typically do not need to do this step.

Instructions for Linux or Mac

export PATH=$PATH:$GOPATH/bin

Go Workspaces

Under the $GOPATH there are three folders:

.
├── bin
├── pkg
└── src

bin

This is where compiled Go programs will be installed.

pkg

Compiled package objects. You can safely ignore this directory.

src

This is where all of your source code for Go projects has to live.

Common Layout

It is common to layout out your Go project in the following directory structure:

$GOPATH/src/github.com/username/project

This will make projects available with the go get tool. It will also help readability later.

Exercise: System Check

package main

import (
	"log"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		log.Fatal("Your GOPATH has not been set!")
	}

	path := os.Getenv("PATH")
	gobin := filepath.Join(gopath, "bin")
	if !strings.Contains(path, gobin) {
		log.Fatalf("Your PATH does not contain %s", gobin)
	}

	log.Println("Success!")
}

file: ./src/env_check.go

package main

import (
	"log"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		log.Fatal("Your GOPATH has not been set!")
	}

	path := os.Getenv("PATH")
	gobin := filepath.Join(gopath, "bin")
	if !strings.Contains(path, gobin) {
		log.Fatalf("Your PATH does not contain %s", gobin)
	}

	log.Println("Success!")
}

file: ./src/env_check.go

$ go run main.go

If you see anything other than Success, your GOPATH is not correctly set up.

Optional Steps

Revision Control Systems

Go has the ability to import remote packages via revision control systems with the go get command. To ensure that you can retrieve any remote package, be sure to install the following rcs software to your system.

VPN Issues

Many VPN's block access to sites like gihtub.com, specifically when you are at the command line and using git commands.

If you are experiencing issues with setting up, or retrieving code repositories with git or the go get command, check your VPN settings or connect to a public network to issue the commands.