Go基本安装及环境变量说明

john · · 7683 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Go的安装与配置在官方已经有很详尽的说明,安装说明请参考官方链接:https://golang.org/doc/install

为方便开发,在开发环境的安装中需要注意的是个三个环境变量的设置:

1、$GOROOT:go的安装目录,配置后不会再更改;

2、$PATH:需要将go的bin目录添加到系统$PATH中以便方便使用go的相关命令,配置后也不会再更改;

3、$GOPATH:go项目在本地的开发环境的的项目根路径(以便项目编译,go build, go install),不同的项目在编译的时候该环境变量可以不同

GO的环境变量在官方文档中也有详情的说明,请参考链接:https://golang.org/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.exe的windows x86架构的可执行文件:


有疑问加站长微信联系(非本文作者)

本文来自:johng

感谢作者:john

查看原文:Go基本安装及环境变量说明

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

7683 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传