GOPATH is an environment variable in Go programming. It specifies the location of your workspace. It is the directory that contains your Go source code, binary files, and package objects.

The line export PATH=$(PATH):$(GOPATH)/bin; is adding the bin directory inside your GOPATH to your system's PATH.

PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. By adding $(GOPATH)/bin to PATH, it allows the system to find and execute Go binaries at any location from the terminal.

Traditionally, if you switch between different Go projects that reside in different locations, you would need to manually change the GOPATH environment variable to point to the correct workspace.

However, since Go 1.11, the Go team introduced a feature called Go Modules which allows for the automatic retrieval of dependencies, and it also eliminates the need for a GOPATH. With Go Modules, you can work with Go projects located anywhere on your file system.

How to use Go Modules?

In .zshrc , .bashrc , or .bash_profile file

# The dependency in Go indicate whether using Go modules
export GO111MODULE=on

off: The go command ignores the go.mod file and looks in vendor directories and the GOPATH. on: The go command uses the go.mod file and ignores the GOPATH. auto: (default) Go modules are enabled only if a go.mod file is present in your project or any parent directory.

# Indicate GOPATH to the directory that install Go.
export GOPATH=$HOME/go
# Allows the system find the executable Go golbal library
export PATH=$PATH:$GOPATH/bin

Go Modules Commands

# Initialize a new module
go mod init path/to/my/workplace

# Add dependencies
go get github.com/user/repo
# Upgrade dependencies
go get -u github.com/user/repo
# specify the version of dependencies
go get github.com/user/repo@v1.2.3

# Remove unused dependencies
go mod tidy

Install Library

(After Go 1.16)

go get: It will change dependencies to the current module or to upgrade or downgrade an existing dependency. Updates the go.mod and go.sum files to reflect the downloaded versions. go install: Compiles the package source code and places the resulting executable in the appropriate location (usually $GOPATH/bin). If necessary, downloads remote packages and their dependencies before building.

Feature go get go install
Primary function Manage dependencies Build and install packages
Builds packages No (since Go 1.18) Yes
Installs packages No (since Go 1.18) Yes
Updates go.mod/go.sum Yes No
Downloads dependencies Yes Yes (if not already downloaded)