Frequently Used Predefined Environment Variables in Go
Go has some predefined environment variables that are frequently used and should be taken into account, as they will be useful in our projects. For example, GOOS will give us the operating system we are on.
More used
- GOROOT: – Location of the Go SDK (fixed).
- GOPATH: – Root directory of your Go workspace (customizable).
- CGO_ENABLED: – Controls Go’s integration with C code (1 to enable, 0 to disable).
- GO111MODULE: – Controls the mode of using Go modules (auto, on, off).
- GOOS: – Operating system
- GOARCH: – Architecture
Less frequently used:
- GOPROXY and GOSUMDB: Important for dependency management in restricted networks and to ensure module integrity.
- GOCACHE: Beneficial for improving compilation times in large projects.
- GOTMPDIR and GOMODCACHE: Useful in advanced development configurations.
Next, we will delve deeper into these variables.
GOROOT
The location where Go is installed is set by default unless you use a binary distribution.
You can always know the value of GOROOT with the following command:
CMD or Terminalgo env GOROOT
Set GOROOT on Windows
This can be set or defined temporarily on Windows with:
CMD or Terminalset GOROOT=C:\path\to\your\goroot
It can also be set permanently with:
CMD or Terminalsetx GOROOT=C:\path\to\your\goroot
set GOROOT on Linux
We can define GOROOT temporarily in Linux as follows:
CMD o Terminalexport GOROOT=C:\path\to\your\goroot
We can also define GOROOT permanently in our .bashrc or .zshrc file by adding the export line
.bashrc or .zshrc#add the following line export GOROOT=C:\path\to\your\goroot
You will need to reload the .bashrc or .zshrc file as a source.
CMD o Terminalsource .bashrc
GOPATH (en des-uso)
Root directory of your Go workspace, it has currently been replaced by modules, but you might still need it.
You can get the value of GOPATH with the following command:
CMD o Terminalgo env GOPATH
Set GOPATH on Windows
It will set the GOPATH variable temporarily
CMD o Terminalset GOPATH=C:\path\to\your\gopath
It will set the GOPATH variable permanently
CMD o Terminalsetx GOPATH=C:\path\to\your\gopath
Set GOPATH on Linux
Temporary
CMD o Terminalexport GOPATH=C:\path\to\your\gopath
Permanently
.bashrc or .zshrc#add the following line export GOPATH=C:\path\to\your\gopathCMD o Terminal
source .bashrc
CGO_ENABLED
Controls the integration of Go with C code. In other words, this variable enables the use of C code in Go.
It can have two values: 1 or 0. To use C, it must be set to 1. It is recommended to keep it set to 1 permanently (default).
CMD or Terminalgo env CGO_ENABLED
Set CGO_ENABLED on Windows
Temporal
CMD or Terminalset CGO_ENABLED=1
Permanently
CMD or Terminalsetx CGO_ENABLED=1
Set CGO_ENABLED on Linux
Temporary
CMD or Terminalexport CGO_ENABLED=1
Permanently
.bashrc# add this line to your bashrc export CGO_ENABLED=1CMD or Terminal
source .bashrc
GO111MODULE
GO111MODULE indicates whether modules are enabled or not; if not, Go will use GOPATH. The GO111MODULE variable has three possible values: on, off, and auto.
If set to auto, it will check if a go.mod file exists; if not, it will use GOPATH.
go env GO111MODULE
Set GO111MODULE on Windows
Temporal
CMD or Terminalset GO111MODULE=on
Permanently
CMD or Terminalsetx GO111MODULE=on
Set GO111MODULE on Linux
Temporary
CMD or Terminalexport GO111MODULE=on
To make the change permanent, you will need to add the export to your .bashrc.
.bashrc# add this line to your bashrc export GO111MODULE="on"CMD o Terminal
source .bashrc
GOOS and GOARCH
You can always check your architecture with GOARCH and your OS with GOOS. Obviously, this is not a value you can change.
CMD or Terminalgo env GOOS go env GOARCH
You can detect the user’s OS who is using your program with runtime.
if runtime.GOOS == "windows" { fmt.Println("Estas usando windows") }
GOCACHE
The Go cache command creates outputs for reuse in future builds. By default, Go uses a sub-folder named ‘go-build’ in the system user’s cache folder (OS).
The GOCACHE variable allows us to specify another location for this data.
CMD or Terminalgo env GOCACHE
GOMODCACHE
The GOMODCACHE variable specifies where the module cache will be stored; the default path is $GOPATH/pkg/mod.
CMD or Terminalgo env GOMODCACHE
GOTMPDIR
If the GOTMPDIR variable is defined, temporary files will be created in that directory.
Otherwise, they will be created in the OS’s temporary files.
CMD or Terminalgo env GOTMPDIR
GOSUMDB
The GOSUMDB environment variable in Go is used to specify the checksum database that the Go toolchain should use to verify the integrity of modules.
It checks the checksums of modules that are not yet listed in go.sum.
There may be occasions where the proxy and checksum cannot be reached. In this case, it is recommended to disable it using the following configuration.
go env -w GOPROXY=direct go env -w GOSUMDB=off
You can see the path of GOSUMDB with:
CMD o Terminalgo env GOSUMDB
GOPROXY
In general, this is a variable we shouldn’t change, except in some cases when necessary.
Default value: https://proxy.golang.org,direct
CMD or Terminalgo env GOPROXY
go env
.