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 addingBIN
to your existingPATH
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.
Go Workspaces
Common Layout
It is common to lay out 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.
Creating Your Workspace
Because we are explicitly setting the GOPATH
, we should create it prior
to setting it:
mkdir -p $HOME/go/{bin,pkg,src}
Setting Up The $GOPATH
$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
Exercise: System Check
Close and reopen your terminal to ensure that all environment variables are refreshed.
Create a file called main.go
and put the following content in it:
(the file can be located anywhere on your system and will still work for this test)
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!")
}
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!")
}
Run the following command from the directory you placed the main.go
file in:
$ 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.
Editors
Visual Studio Code
Sublime
- www.sublimetext.com
- github.com/DisposaBoy/GoSublime
- www.wolfe.id.au/2015/03/05/using-sublime-text-for-go-development/
Vim
Goland (formerly known as Gogland)
Atom
LiteIDE
Emacs
More Editors
For a full list of editors, see the wiki
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.