Golang Environment variables
1. Core Build and Runtime Environment Variables
-
GOROOT
- Purpose: Specifies the root directory where Go is installed.
- Default: Set automatically by Go; generally not set manually unless using a custom Go installation.
- Example:
/usr/local/go
-
GOPATH
- Purpose: The workspace directory for Go, which contains
src
,pkg
, andbin
directories. - Default:
$HOME/go
if not set manually.
- Purpose: The workspace directory for Go, which contains
-
GOOS
- Purpose: Sets the target operating system for builds.
- Values:
linux
,darwin
,windows
,freebsd
, etc. - Usage: Can be used to cross-compile for different platforms.
-
GOARCH
- Purpose: Sets the target CPU architecture for builds.
- Values:
amd64
,386
,arm
,arm64
,ppc64
, etc. - Usage: Used for cross-compiling for different architectures.
-
GOMOD
- Purpose: Shows the path to the
go.mod
file if the project uses modules. - Example:
/path/to/your/project/go.mod
- Purpose: Shows the path to the
2. Module-Related Variables
-
GOPROXY
- Purpose: Sets the URL for the Go module proxy, which fetches dependencies.
- Default:
https://proxy.golang.org
- Example:
direct
(to fetch directly from source repositories).
-
GOSUMDB
- Purpose: Specifies the checksum database for verifying module authenticity.
- Default:
sum.golang.org
- Usage: Set to
off
to disable checksum validation.
-
GOMODCACHE
- Purpose: Sets the location for the Go module cache, where downloaded modules are stored.
- Default:
$GOPATH/pkg/mod
-
GOINSECURE
- Purpose: Specifies modules or repositories to skip the checksum validation.
- Example:
example.com/insecure/module
-
GOPRIVATE
- Purpose: Lists private modules or repositories, skipping proxy and checksum checks for these modules.
- Example:
mycompany.com/private/...
-
GO111MODULE
- Purpose: Controls the use of modules.
- Values:
auto
(default, enabling modules based on environment),on
(always enable),off
(disable).
3. Debugging and Performance Variables
-
GOCACHE
- Purpose: Directory for storing cached Go build data.
- Default:
$HOME/.cache/go-build
-
GODEBUG
- Purpose: Enables various runtime debugging options.
- Example:
gctrace=1
(logs garbage collection information),schedtrace=1000
(logs scheduling information every 1000 ms)
-
GOTRACEBACK
- Purpose: Controls the level of detail in stack traces when a program crashes.
- Values:
none
,single
,all
,system
- Example:
all
for complete stack traces.
-
GOMAXPROCS
- Purpose: Limits the maximum number of CPUs that can execute Go code simultaneously.
- Default: Equal to the number of CPUs available on the system.
4. Build Configuration Variables
-
CGO_ENABLED
- Purpose: Enables or disables
cgo
, allowing Go to call C code. - Values:
0
(disabled),1
(enabled) - Default:
1
on most platforms,0
when cross-compiling.
- Purpose: Enables or disables
-
GOFLAGS
- Purpose: Sets global flags for all
go
commands. - Example:
-mod=readonly
to preventgo.mod
from being modified.
- Purpose: Sets global flags for all
-
GOTMPDIR
- Purpose: Directory for temporary files used during builds.
- Default: System temp directory, like
/tmp
on Unix.
-
GOBIN
- Purpose: Directory where
go install
saves executable binaries. - Default:
$GOPATH/bin
- Purpose: Directory where
-
GOEXPERIMENT
- Purpose: Enables experimental features in Go; generally used for internal testing.
- Example:
loopvar
5. Testing and CI/CD Related Variables
-
GOTEST
- Purpose: Used to pass additional arguments to
go test
. - Example:
-v
for verbose test output.
- Purpose: Used to pass additional arguments to
-
GOTESTFLAGS
- Purpose: Specifies flags for
go test
. - Example:
-timeout=1m
to set a timeout for tests.
- Purpose: Specifies flags for
-
GOEXE
- Purpose: Sets the file extension for executables (useful when cross-compiling).
- Default: Empty on Unix,
.exe
on Windows.
-
GOTRACEBACK
- Purpose: Controls how much detail is included in a stack trace when the program crashes.
- Example:
none
,single
,all
,system
6. Compiler and Linker Control Variables
-
GO_LDFLAGS
- Purpose: Flags passed to the linker, used to control the linking process.
- Example:
-s -w
to reduce binary size by omitting symbol tables.
-
GO_GCFLAGS
- Purpose: Flags for the Go compiler, particularly useful for debugging and controlling optimizations.
- Example:
-l
to disable optimizations for better debugging.
-
GO_LINKTOOL
- Purpose: Specifies a custom linker to use for Go builds.
- Default: Uses the default Go linker unless overridden.
7. Miscellaneous Variables
-
GOVERSION
- Purpose: Displays the currently installed version of Go.
- Example:
go1.20
-
GOOS
- Purpose: Sets the operating system target for cross-compiling (repeat).