Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished | checked

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.
This section I’m creating for personal use, but since it might be helpful for others, I’ve put it online. In this section, you’ll see how to obtain the predefined environment variables one by one, but know that you can also get them all at once like this go env.

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 Terminal
go env GOROOT

Set GOROOT on Windows

This can be set or defined temporarily on Windows with:

CMD or Terminal
set GOROOT=C:\path\to\your\goroot

It can also be set permanently with:

CMD or Terminal
setx GOROOT=C:\path\to\your\goroot

set GOROOT on Linux

We can define GOROOT temporarily in Linux as follows:

CMD o Terminal
export 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 Terminal
source .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 Terminal
go env GOPATH

Set GOPATH on Windows

It will set the GOPATH variable temporarily

CMD o Terminal
set GOPATH=C:\path\to\your\gopath

It will set the GOPATH variable permanently

CMD o Terminal
setx GOPATH=C:\path\to\your\gopath

Set GOPATH on Linux

Temporary

CMD o Terminal
export GOPATH=C:\path\to\your\gopath

Permanently

.bashrc or .zshrc
#add the following line
export GOPATH=C:\path\to\your\gopath
CMD 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 Terminal
go env CGO_ENABLED

Set CGO_ENABLED on Windows

Temporal

CMD or Terminal
set CGO_ENABLED=1

Permanently

CMD or Terminal
setx CGO_ENABLED=1

Set CGO_ENABLED on Linux

Temporary

CMD or Terminal
export CGO_ENABLED=1

Permanently

.bashrc
# add this line to your bashrc
export CGO_ENABLED=1
CMD 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.

Puedes ver una explicacion mas completa de GO111Module en https://rambabuy.medium.com/understanding-go-go111module-16917777053c
CMD or Terminal
go env GO111MODULE

Set GO111MODULE on Windows

Temporal

CMD or Terminal
set GO111MODULE=on

Permanently

CMD or Terminal
setx GO111MODULE=on

Set GO111MODULE on Linux

Temporary

CMD or Terminal
export 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 Terminal
go 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 Terminal
go env GOCACHE

GOMODCACHE

The GOMODCACHE variable specifies where the module cache will be stored; the default path is $GOPATH/pkg/mod.

CMD or Terminal
go 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 Terminal
go 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 Terminal
go 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 Terminal
go env GOPROXY
Category: en-go
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments