Golang Environment and Golang Command

wahyu eko hadi saputro
3 min readApr 13, 2023

--

Go Environtment

Environment variables are a way for programs to access information about the environment in which they are running, such as the operating system, network settings, and user preferences. In the case of Go, environment variables can be used to set important configuration options such as the location of Go modules and the Go path.

env variable golang
Some common environment variables used in Golang include:
GOPATH: This specifies the location of the Go workspace, which is the directory where Go code is stored and compiled.
GOBIN: This specifies the directory where compiled Go binaries should be installed.
GOROOT: This specifies the location of the Go installation directory on the local machine.
GOOS and GOARCH: These environment variables specify the target operating system and architecture for the Go program being compiled.
GO111MODULE is an environment variable used by the Go programming language to enable or disable module support.

GO111MODULE can be set in two different ways:
- The environment variable in your shell : export GO111MODULE=on.
- The “hidden” global configuration only known by go env using

go env -w GO111MODULE=on (only available since Go 1.12).

When GO111MODULE is set to on, Go modules are enabled, which means that the Go toolchain will use the go.mod file located in the project directory to manage dependencies and go get command will download dependency on $GOPATH/pkg/

$GOPATH/pkg/

When GO111MODULE is set to off, Go modules are disabled, and the legacy GOPATH-based method of managing dependencies is used and go get command will download dependency on $GOPATH/src/

$gopath/src/

By setting these environment variables, developers can customize their Go development environment to suit their needs and ensure that their code is compiled and run correctly.

Go Command

The go command is a tool provided by the Go programming language to manage Go code and packages. It is the primary interface for working with Go code and is used for tasks such as building, testing, installing, and managing dependencies.

Some of the most commonly used subcommands of the go command include:
go build: This command compiles Go code and creates a binary executable file.
go test: This command runs tests for a Go package.
go install: This command installs a package or binary to the local machine, making it available for use in other Go projects.
go get: This command downloads and installs a package and its dependencies from a remote repository.
go mod: This command manages Go modules, which are a way to manage dependencies in Go projects.

In addition to these subcommands, the go command has many other features and options that can be used to customize its behavior. For example, the go run command can be used to compile and execute a Go program in a single step, and the go doc command can be used to display documentation for Go packages and functions.

--

--

No responses yet