Learn Go: Golang Installation, Setup, GOPATH, and Go Workspace

Setting up development environment for Golang development

Gopher

Lets go!!

Published · Jan 12


This is the first part of our tutorial series on Go. In this article, you’ll learn how to install Go in your system and set up your development environment for Go projects.

Installing Go

Go binary distributions are available for all major operating systems like Linux, Windows, and MacOS. It’s super simple to install Go from the binary distributions.

If a binary distribution is not available for your operating system, you can try installing Go from source.

Mac OS X


Using Homebrew
The easiest way to install Go in Mac OS is by using Homebrew -
brew install go


Using macOS package installer
Download the latest Go package (.pkg) file from Go’s official downloads page. Open the package and follow the on-screen instructions to install Go. By default, Go will be installed in /usr/local/go.

Linux

Download the Linux distribution from Go’s official download page and extract it into /usr/local directory.
sudo tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
Note: Replace $VERSION.$OS-$ARCH.tar.gz with the zip you have downloaded from the Go’s official download page

Next, add the /usr/local/go/bin directory to your PATH environment variable. Add /usr/local/go/bin to the PATH environment variable.

You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:
sudo nano ~/.profile
Add the following line at the bottom of the file
export PATH=$PATH:/usr/local/go/bin

Refresh the profile with follwong command
sudo source ~/.profile

You can also use any other directory like /opt/go instead of /usr/local for installing Go.

Windows

Download the Windows MSI installer file from Go’s official download page. Open the installer and follow the on-screen instructions to install Go in your windows system. By default, the installer installs Go in C:\Go

. . .

The Go tool

After installing Go by following the instructions in the previous section, you should be able to run the Go tool by typing go in the command line -
$ go Go is a tool for managing Go source code. Usage: go command [arguments] The commands are: build compile packages and dependencies clean remove object files doc show documentation for package or symbol env print Go environment information bug start a bug report fix run go tool fix on packages fmt run gofmt on package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet run go tool vet on packages Use "go help [command]" for more information about a command. Additional help topics: c calling between Go and C buildmode description of build modes filetype file types gopath GOPATH environment variable environment environment variables importpath import path syntax packages description of package lists testflag description of testing flags testfunc description of testing functions Use "go help [topic]" for more information about that topic.
. . .

GOPATH, Go Workspace, and Go Code Organization

Go requires you to organize your code in a specific way -
By convention, all your Go code and the code you import, must reside in a single workspace.

A workspace is nothing but a directory in your file system whose path is stored in the environment variable GOPATH.

The workspace directory contains the following sub directories at its root -
  • src: contains Go source files. The src directory typically contains many version control repositories containing one or more Go packages. Every Go source file belongs to a package. You generally create a new subdirectory inside your repository for every separate Go package.
  • bin: contains the binary executables. The Go tool builds and installs binary executables to this directory. All Go programs that are meant to be executables must contain a source file with a special package called main and define the entry point of the program in a special function called main().
  • pkg: contains Go package archives (.a). All the non-executable packages (shared libraries) are stored in this directory. You cannot run these packages directly as they are not binary files. They are typically imported and used inside other executable packages.
# A typical Go workspace bin/ myapp # Executable binary hello # Executable binary pkg/ github.com/gorilla/ mux.a # Package archive go.uber.org/ zap.a # Package archive src/ github.com/gorilla/mux/ # 3rd Party package #... package contents go.uber.org/zap/ # 3rd Party package #... package contents hello/ # Local package (not published anywhere) hello.go # ... (more repositories and packages omitted) ...
. . .

Setting GOPATH

The GOPATH environment variable specifies the location of your workspace. By default, the GOPATH is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you’re happy with this path then you don’t need to do anything. You can just create your workspace directory named go inside the home folder and start writing Go code.

If you want to use a custom location as your workspace, you can set the GOPATH environment variable by following the instructions below -


Unix Systems (Linux and macOS)

For setting GOPATH in bash shells, add the following line to the ~/.profile file -
sudo nano ~/.profile

If you use Zsh shell, then you need to add the above line to ~/.zshrc file.
Add the following line at the bottom of the file
export GOPATH=$HOME/go

Refresh the profile with follwong command
sudo source ~/.profile

Create workspace folders
mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin

Windows System

Let’s say that you want to have your workspace directory at C:\go-workspace. Here is how you can set the GOPATH environment variable to use this workspace location -
  • Create the workspace folder at C:\go-workspace.
  • Right click on Start → click Control Panel → Select System and Security → click on System.
  • From the menu on the left, select the Advanced systems settings.
  • Click the Environment Variables button at the bottom.
  • Click New from the User variables section.
  • Type GOPATH into the Variable name field.
  • Type C:\go-workspace into the Variable value field.
  • Click OK.

Note that, GOPATH must be different than the path of your Go installation.
. . .

Testing your Go installation with the Hello World program

First, make sure that you have created the Go workspace directory at $HOME/go. Next, create a new directory src/hello inside your workspace. Finally, create a file named hello.go with the following code -

Note: after Go 1.11 onwards, you can also create and run the program outside of Go workspace.

$ cd $HOME/go/src/hello

make a file hello.go with this program
package main import "fmt" func main() { fmt.Printf("Hello, World\n") }

$ ls hello.go

The easiest way to run the above program is using the go run command -
$ go run hello.go Hello, World


Building an executable binary using go build

The go run command compiles and runs your program at one go. If however, you want to produce a binary from your Go source that can be run as a standalone executable without using the Go tool, then use the go build command -
$ cd $HOME/go/src/hello $ go build $ ls hello hello.go

The go build command creates an executable binary with the same name as the name of your immediate package (hello). You can run the binary file like so -
$ ./hello Hello, World

. . .


Conclusion

That’s all for now folks! I hope you’re all set to deep dive and learn more about the Go programming language. You can find all the tutorials written in Go from the Golang categories page.

Thanks for reading, see you in the next article.

Never miss a post from Gopher , when you sign up for Ednsquare.