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"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_v/4r515ktx08g5yr6dgkxhfyfr0000gn/T/go-build802577064=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
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/<username>/code/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_v/4r515ktx08g5yr6dgkxhfyfr0000gn/T/go-build802577064=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
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.
VIM
http://www.vim.org/download.php
Victor Farazdagi wrote this post on installing Vim for Go:
http://farazdagi.com../../2015/vim-as-golang-ide/
Visual Studio Code
https://code.visualstudio.com
Luke Hobah wrote an amazing plugin for VS Code:
https://github.com/Microsoft/vscode-go
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
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
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