Searching...

[TOC]

Go环境变量

为方便开发,在开发环境往往需要设置三个环境变量:

  1. $GOROOT:go的安装目录,配置后不会再更改;
  2. $GOPATH:go项目在本地的开发环境的的项目根路径(以便项目编译,go build, go install),不同的项目在编译的时候该环境变量可以不同;
  3. $PATH(重要):需要将go的bin目录添加到系统$PATH中以便方便使用go的相关命令,配置后也不会再更改;

Go的环境变量在官方文档中也有详情的说明,请参考链接:https://golang.google.cn/doc/install/source

$GOROOT
    The root of the Go tree, often $HOME/go1.X. Its value is built into the tree when it is compiled, and defaults to the parent of the directory where all.bash was run. There is no need to set this unless you want to switch between multiple local copies of the repository.


$GOROOT_FINAL
    The value assumed by installed binaries and scripts when $GOROOT is not set explicitly. It defaults to the value of $GOROOT. If you want to build the Go tree in one location but move it elsewhere after the build, set $GOROOT_FINAL to the eventual location.


$GOOS and $GOARCH
    The name of the target operating system and compilation architecture. These default to the values of $GOHOSTOS and $GOHOSTARCH respectively (described below).

    Choices for $GOOS are darwin (Mac OS X 10.8 and above and iOS), dragonfly, freebsd, linux, netbsd, openbsd, plan9, solaris and windows. Choices for $GOARCH are amd64 (64-bit x86, the most mature port), 386 (32-bit x86), arm (32-bit ARM), arm64 (64-bit ARM), ppc64le (PowerPC 64-bit, little-endian), ppc64 (PowerPC 64-bit, big-endian), mips64le (MIPS 64-bit, little-endian), and mips64 (MIPS 64-bit, big-endian). mipsle (MIPS 32-bit, little-endian), and mips (MIPS 32-bit, big-endian). The valid combinations of $GOOS and $GOARCH are:

    $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
    netbsd     386
    netbsd     amd64
    netbsd     arm
    openbsd    386
    openbsd    amd64
    openbsd    arm
    plan9      386
    plan9      amd64
    solaris    amd64
    windows    386
    windows    amd64


$GOHOSTOS and $GOHOSTARCH
    The name of the host operating system and compilation architecture. These default to the local system's operating system and architecture.
    
    Valid choices are the same as for $GOOS and $GOARCH, listed above. The specified values must be compatible with the local system. For example, you should not set $GOHOSTARCH to arm on an x86 system.
    

$GOBIN
    The location where Go binaries will be installed. The default is $GOROOT/bin. After installing, you will want to arrange to add this directory to your $PATH, so you can use the tools. If $GOBIN is set, the go command installs all commands there.


$GO386 (for 386 only, default is auto-detected if built on either 386 or amd64, 387 otherwise)
    This controls the code generated by gc to use either the 387 floating-point unit (set to 387) or SSE2 instructions (set to sse2) for floating point computations.
    
    GO386=387: use x87 for floating point operations; should support all x86 chips (Pentium MMX or later).
    GO386=sse2: use SSE2 for floating point operations; has better performance than 387, but only available on Pentium 4/Opteron/Athlon 64 or later.


$GOARM (for arm only; default is auto-detected if building on the target processor, 6 if not)
    This sets the ARM floating point co-processor architecture version the run-time should target. If you are compiling on the target system, its value will be auto-detected.
    
    GOARM=5: use software floating point; when CPU doesn't have VFP co-processor
    GOARM=6: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
    GOARM=7: use VFPv3; usually Cortex-A cores
    If in doubt, leave this variable unset, and adjust it if required when you first run the Go executable. The GoARM page on the Go community wiki contains further details regarding Go's ARM support.

环境变量中的$GOOS$GOARCH是比较实用的两个变量,可以用在不同平台的交叉编译中,只需要在go build之前设置这两个变量即可,这也是go语言的优势之一:可以编译生成跨平台运行的可执行文件。感觉比QT更高效更轻量级,虽然生成的可执行文件是大了一点,不过也在可接受的范围之内。 例如,在Linux amd64架构下编译Windows x86的可执行文件,可以使用如下命令:

CGO_ENABLED=0 GOOS=windows GOARCH=386 go build hello.go

遗憾的是交叉编译暂不支持cgo方式,因此需要将环境变量$CGO_ENABLED设置为0,这样执行之后会在当前目录生成一个hello.exewindows x86架构的可执行文件。

环境变量设置

除了$PATH环境外,其他环境变量都是可选的。

为什么说这个步骤可选呢?因为未来的Go版本慢慢开始移除对$GOPATH/$GOROOT的支持。此外,在Goland这个IDE中集成有Terminal功能,直接使用这个功能中已经设置好了环境变量。

*nix下设置环境变量

*nix系统下(Linux/Unix/MacOS/*BSD等等),需要在/etc/profile中增加以下环境变量设置,重新登录的时候便会自动添加到用户的环境变量中:

export GOROOT=/usr/local/go
export GOPATH=/Users/john/Workspace/Go/GOPATH
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Windows下设置环境变量

Windows如何修改系统环境变量,以及修改环境变量PATH,请参考网上教程(百度Google)。