Golang Installation, Setup, GOPATH, and Go Workspace

Go is an open source, statically typed, compiled programming language built by Google.

It combines the best of both statically typed and dynamically typed languages and gives you the right mixture of efficiency and ease of programming. It is primarily suited for building fast, efficient, and reliable server side applications.

Following are some of the most noted features of Go -

  • Safety : Both Type safety and Memory safety.
  • Good support for Concurrency and communication.
  • Efficient and latency-free Garbage Collection
  • High speed compilation
  • Excellent Tooling support

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.


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

Next, add the /usr/local/go/bin directory to your PATH environment variable. You can do this by adding the following line to your ~/.bash_profile file -

export PATH=$PATH:/usr/local/go/bin

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


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

The Go distribution comes bundled with the go tool. It is a command line tool that lets you automate common tasks such as downloading and installing dependencies, building and testing your code, and much more.

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.


	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.

Note that, after the introduction of Go modules in Go 1.11, you’re no longer required to store your Go code in the Go workspace. You can create your Go project in any directory outside of GOPATH. The following explanation of Go workspace is given for historical reasons and the fact that it’s still valid. You can skip this section if you want.

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
    myapp							# Executable binary
    hello						    # Executable binary
       numbers.a                    # Package archive
       strings.a                    # Package archive
       mux.a                        # Package archive
       zap.a                        # Package archive
    github.com/callicoder/example/  # Project repository
          app.go                    # Executable program containing main package and function
       numbers/                     # Go Package (contains utility functions for working with numbers)
       strings/                     # Go Package (contains utility functions for working with strings)
    github.com/gorilla/mux/			# 3rd Party package
       #... package contents
    go.uber.org/zap/				# 3rd Party package
       #... package contents
    hello/     						# Local package (not published anywhere)

	# ... (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 ~/.bash_profile file -

export GOPATH=$HOME/go

If you use Zsh shell, then you need to add the above line to ~/.zshrc file.

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.

package main

import "fmt"

func main() {
	fmt.Printf("Hello, World\n")
$ cd $HOME/go/src/hello
$ ls

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

Installing the package into the bin directory using go install

You can use the go install command to build and install the executable binary into your workspace’s bin directory -

$ cd $HOME/go/src/hello
$ go install
$ cd $HOME/go/bin
$ ls
$ ./hello
Hello, World

You can also add the $HOME/go/bin directory to the PATH variable to run go executables from any location.

Don’t forget to check out: go help run, go help build, go help install.


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.

Next Article: Hello Golang: Writing your first Golang Program

Code Samples: github.com/callicoder/golang-tutorials

More resources