Golang
Golang Development Environment Setup
Setting up a Golang environment is easy and can be done in a matter of minutes. Depending on your operating system, follow the instructions below:
MacOS (using Homebrew)
-
Install Homebrew (if you haven’t already…Just go here for instructions
-
Install Go
With Homebrew installed, you can now install Go:
Terminal window brew install go -
Verify Installation
To ensure Go is installed correctly:
Terminal window go versionThis should display the version of Go that you installed.
-
Setup Go Workspace
While newer versions of Go do not strictly require you to have a GOPATH, it’s still a good practice to have one. You can set it up as:
Terminal window echo 'export GOPATH=$HOME/go' >> ~/.zshrcecho 'export PATH=$PATH:$GOPATH/bin' >> ~/.zshrcsource ~/.zshrcNote: If you’re using bash instead of zsh, replace
.zshrcwith.bash_profile.
Windows (using Scoop)
-
Install Scoop (if you haven’t already)
Open a PowerShell window and run:
Terminal window Set-ExecutionPolicy RemoteSigned -scope CurrentUseriwr -useb get.scoop.sh | iex -
Add the “extras” bucket (if you haven’t already)
The
extrasbucket contains the Go app manifest and other useful software:Terminal window scoop bucket add extras -
Install Go
Now you can install Go using Scoop:
Terminal window scoop install go -
Verify Installation
To ensure Go is installed correctly:
Terminal window go versionThis should display the version of Go that you installed.
-
Setup Go Workspace
Although Go modules have made GOPATH less necessary, you might still want to set it:
Terminal window $env:GOPATH = "$HOME\go"[System.Environment]::SetEnvironmentVariable('GOPATH', $env:GOPATH, [System.EnvironmentVariableTarget]::User)[System.Environment]::SetEnvironmentVariable('PATH', $env:PATH + ";$env:GOPATH\bin", [System.EnvironmentVariableTarget]::User)
Configuring Go
When setting up your Go environment, there are various environment variables you can set to influence the behavior of Go tools. Below is a table describing the primary environment variables and their intended use:
| Name | Value | Function |
|---|---|---|
CGO_ENABLED | 0 | Disables the cgo command to reduce possible attack vectors (e.g. CVE-2020-28366 and CVE-2020-28367) which may be exploited by external modules. |
GO_PRIVATE | (your value i.e. yourrepo.internal.com/*) | Comma-separated list of glob patterns (in the syntax of Go’s path.Match) of module path prefixes that should always be fetched directly from their origin and never through a proxy. |
GOPROXY | (your value: i.e. Your internal nexus https://nexus.internal/read-go) | A comma-separated list of proxy URLs checked in sequence. This is used to download modules and verify their checksums. |
GOSUMDB | (your value: i.e. sum.golang.org https://nexus.internal/raw-read-go-sum) | Indicates which checksum database to use. This can be set to off to disable the database check entirely. |
GOVCS | (your value: i.e. *:off) | Configures which version control systems are allowed for which module path patterns. |
no_proxy | (your value: i.e: .foo.bar.com,.foo.internal) | Comma-separated list of domain names or IP addresses that shouldn’t use a proxy. Useful for local development or private repositories. |