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.
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:
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.
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
$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 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
Mac and Linux Users
Create folders named
code/go/src in your
cd $HOME mkdir -p ~/code/go/src
.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:
Create folders named
code\go\src in your
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
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:
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
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
To validate the
bin folder was configured properly with your system
PATH, try to run the
$ 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.
There are a lot of options and plugins for many of the popular editors.
Victor Farazdagi wrote this post on installing Vim for Go:
Visual Studio Code
Luke Hobah wrote an amazing plugin for VS Code:
Joe Fitzgerald wrote this plugin for Atom
This is the configuration Guillaume Charmes uses:
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.
Go In Action
If you would like to try out the book, send me an email:
Ultimate Go Programming LiveLessons
Ultimate Go Programming LiveLessons provides an intensive, comprehensive, and idiomatic view of the Go programming language. This course focuses on both the specification and implementation of the language, including topics ranging from language syntax, design, and guidelines to concurrency, testing, and profiling. This class is perfect for anyone who wants a jump-start in learning Go or wants a more thorough understanding of the language and its internals.Learn more
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.
To learn about Corporate training events, options and special pricing please contact: