Fork me on GitHub

Go环境变量

介绍几个go语言编程中常用的环境变量,都可以通过export指令修改

$GOROOT

go编译器程序的安装目录

$GOPATH

go项目工程的根目录

该目录下必须包含src,pkg,bin3个子目录

$GOBIN

go程序编译完成后生成的可执行binary,使用go install命令时,自动安装到$GOPATH/bin下

$GOOS

$GOARCH

目标平台的操作系统类型和芯片架构类型,这两个参数主要用于交叉编译中

go支持多种操作系统和芯片体系结构,故go对交叉编译也有很好的支持

使用如下命令可以查看当前环境中go相关的环境变量

1
$go env
Linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/lwang/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/lwang/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build193074853=/tmp/go-build -gno-record-gcc-switches"
Mac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bitmain/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bitmain/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5x/6gkjj0_1581fwz9lvylk14mr0000gn/T/go-build089246083=/tmp/go-build -gno-record-gcc-switches -fno-common"
您的鼓励是我持之以恒的动力