這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
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架構的可執行檔: