Explanation and setting of common environment variables in golang

Time:2020-10-25

No matter you are using windows, Linux or Mac OS operating system to develop go applications, after installing the go installation language development tools, you must configure the environment variables required by the go language development to complete the construction of the go development environment.

However, for some beginners, they may not understand the role of environment variables commonly used in go language and how to set environment variables. Let’s talk about it today.

Note: after the installation of the windows operating system installation package (. MSI suffix) provided by go is completed, several commonly used environment variables will be automatically configured.

Common environment variables

There are many environment variables that can be set in go language. Each environment variable has its own function, but many of them may not be used by us. Generally, we need to understand and the most important ones are as follows:

$go env // print all default go environment variables
$go env gopath // print the value of an environment variable

GOROOT

The environment variable goroot represents the installation directory of the go language.

In windows, the default value of goroot is C / go, while in Mac OS or Linux, the default value of goroot is usr / loca / go. If go is installed in other directories, the value of goroot needs to be changed to the corresponding directory.

In addition, goroot / bin contains the tool chain provided by go. Therefore, we should configure goroot / bin into the environment variable path to facilitate us to use the go tool chain globally.

Linux setup goroot demo


export GOROOT=~/go
export PATH=$PATH:$GOROOT/bin

GOPATH

Note that gopath cannot have the same value as goroot.

The environment variable gopath is used to specify our development workspace, which is used to store source code, test files, library static files and executable files.

In UNIX like (MAC OS or Linux) operating systems, the default value of gopath is $home / go. In windows, the default value of gopath is% userprofile% \ \ go (for example, in the admin user, the value is C: \.

Of course, we can change the workspace by modifying gopath. For example, we can set the opt / go mode as follows:

Linux setup gopath demo


export GOPATH=/opt/go

In addition, you can set up multiple workspaces in gopath, such as:


export GOPATH=/opt/go;$home/go

Subdirectories of gopath

The code above indicates that we specify two workspaces. However, when we use the go get command to get the remote library, it is usually installed in the first workspace.

According to the go development specification, each work in gopath directory is generally divided into three subdirectories: SRC, PKG, and bin. Therefore, each workspace we see looks like this:

bin/

Hello ා executable

Executable file

src/

    github.com/golang/example/

        .git/                 

    hello/

hello.go Command line code

    outyet/

main.go Command line code

main_ test.go Test code

    stringutil/

reverse.go Database file

reverse_ test.go Database file

    golang.org/x/image/

        .git/                  

    bmp/

reader.go Database file

writer.go Database file

SRC directory is the source code file we developed, the corresponding directory below is called package, PKG is the compiled library static file, bin is the source code compilation background executable file.

GOBIN

The environment variable Gobin represents the installation directory of binary commands after compiling our development program.

When we use the go install command to compile and package applications, the command will package the compiled binary program into the Gobin directory. Generally, we set Gobin to gopath / bin directory.

Linux setting Gobin demo


export GOBIN=$GOPATH/bin

In the above code, we use the export command to set the environment variables, so that the settings can only be valid in the current shell. If you want to keep the environment variables valid, such as in Linux, you should add the environment variables to the files such as / etc / profile.

Cross compilation

What is cross compilation? The so-called cross compilation refers to the generation of code that can run on another platform on one platform. For example, we can generate binary programs that can run on 64 bit Linux operating system on 32-bit windows operating system development environment.

Cross compilation in other programming languages may require the help of third-party tools, but cross compilation in go language is very simple. The simplest way is to set the environment variables goos and goarch.

Goos and goarch

The default value of goos is our current operating system. For windows and Linux, note that the value of Mac OS operation is Darwin. Goarch means CPU architecture, such as 386, AMD64, arm, etc.

Get the values of goos and goarch

We can use the go env command to get the current values of goos and goarch.


$ go env GOOS GOARCH

Range of goos and goarch

The values of goos and goarch appear in pairs and can only be the values corresponding to the following list.

$GOOS     $GOARCH

android     arm

darwin     386

darwin     amd64

darwin     arm

darwin     arm64

dragonfly   amd64

freebsd     386

freebsd     amd64

freebsd     arm

linux     386

linux     amd64

linux     arm

linux     arm64

linux     ppc64

linux     ppc64le

linux     mips

linux     mipsle

linux     mips64

linux     mips64le

linux     s390x

netbsd     386

netbsd     amd64

netbsd     arm

openbsd     386

openbsd     amd64

openbsd     arm

plan9     386

plan9     amd64

solaris     amd64

windows     386

windows     amd64

Examples

Compiling target programs running on 64 bit Linux operating system


$ GOOS=linux GOARCH=amd64 go build main.go

Compiling the target program of Android operation based on ARM architecture


$ GOOS=android GOARCH=arm GOARM=7 go build main.go

List of environment variables

Although we usually configure only a few environment variables, in fact, go language provides a lot of environment variables, so that we can freely customize the development and compiler behavior.

The following is a list of all the environment variables provided by go. Generally, it can be divided into the following categories. You can have a general understanding of them, because some environment variables can never be used by us.

Through environment variables

GCCGO

GOARCH

GOBIN

GOCACHE

GOFLAGS

GOOS

GOPATH

GOPROXY

GORACE

GOROOT

GOTMPDIR

Environment variables used with CGO

CC

CGO_ENABLED

CGO_CFLAGS

CGO_CFLAGS_ALLOW

CGO_CFLAGS_DISALLOW

CGO_CPPFLAGS, CGO_CPPFLAGS_ALLOW, CGO_CPPFLAGS_DISALLOW

CGO_CXXFLAGS, CGO_CXXFLAGS_ALLOW, CGO_CXXFLAGS_DISALLOW

CGO_FFLAGS, CGO_FFLAGS_ALLOW, CGO_FFLAGS_DISALLOW

CGO_LDFLAGS, CGO_LDFLAGS_ALLOW, CGO_LDFLAGS_DISALLOW

CXX

PKG_CONFIG

AR

Environment variables related to system architecture

GOARM

GO386

GOMIPS

GOMIPS64

Special environment variables

GCCGOTOOLDIR

GOROOT_FINAL

GO_EXTLINK_ENABLED

GIT_ALLOW_PROTOCOL

Other environment variables

GOEXE

GOHOSTARCH

GOHOSTOS

GOMOD

GOTOOLDIR

Summary

The setting of environment variables can affect the process and results of our development and compilation projects, so it is necessary to understand.

For more information about the description and settings of common environment variables in golang, please see the related links below

Recommended Today

Implementation of AOP control transaction by JDK and cglib dynamic proxy

Implementation of AOP control transaction by JDK and cglib dynamic proxy preface Or follow upCustom implementation of IOC container in XML In this instance, there is no transaction operation, and the transaction consistency in a thread operation cannot be maintained; @Override public void transfer(String fromCardNo, String toCardNo, int money) throws Exception { Account from = […]