1. 程式人生 > >Go基本安裝及環境變數說明

Go基本安裝及環境變數說明

為方便開發,在開發環境的安裝中需要注意的是個三個環境變數的設定:

1、$GOROOT:go的安裝目錄,配置後不會再更改;

2、$PATH:需要將go的bin目錄新增到系統$PATH中以便方便使用go的相關命令,配置後也不會再更改;

3、$GOPATH:go專案在本地的開發環境的的專案根路徑(以便專案編譯,go build, go install),不同的專案在編譯的時候該環境變數可以不同

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 $GOROOTTherootoftheGotree,often$HOME/go1.X.Itsvalueisbuiltintothetreewhenitiscompiled,anddefaultstotheparentofthedirectorywhereall.bashwasrun.
Thereisnoneedtosetthisunlessyouwanttoswitchbetweenmultiplelocalcopiesoftherepository.$GOROOT_FINALThevalueassumedbyinstalledbinariesandscriptswhen$GOROOTisnotsetexplicitly.Itdefaultstothevalueof$GOROOT.IfyouwanttobuildtheGotreeinonelocationbutmoveitelsewhereafterthebuild,set$GOROOT_FINALtotheeventuallocation.
$GOOSand$GOARCHThenameofthetargetoperatingsystemandcompilationarchitecture.Thesedefaulttothevaluesof$GOHOSTOSand$GOHOSTARCHrespectively(describedbelow).Choicesfor$GOOSaredarwin(MacOSX10.8andaboveandiOS),dragonfly,freebsd,linux,netbsd,openbsd,plan9,solarisandwindows.Choicesfor$GOARCHareamd64(64-bitx86,themostmatureport),386(32-bitx86),arm(32-bitARM),arm64(64-bitARM),ppc64le(PowerPC64-bit,little-endian),ppc64(PowerPC64-bit,big-endian),mips64le(MIPS64-bit,little-endian),andmips64(MIPS64-bit,big-endian).mipsle(MIPS32-bit,little-endian),andmips(MIPS32-bit,big-endian).Thevalidcombinationsof$GOOSand$GOARCHare:$GOOS$GOARCHandroidarmdarwin386darwinamd64darwinarmdarwinarm64dragonflyamd64freebsd386freebsdamd64freebsdarmlinux386linuxamd64linuxarmlinuxarm64linuxppc64linuxppc64lelinuxmipslinuxmipslelinuxmips64linuxmips64lenetbsd386netbsdamd64netbsdarmopenbsd386openbsdamd64openbsdarmplan9386plan9amd64solarisamd64windows386windowsamd64$GOHOSTOSand$GOHOSTARCHThenameofthehostoperatingsystemandcompilationarchitecture.Thesedefaulttothelocalsystem'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'thaveVFPco-processorGOARM=6:useVFPv1only;defaultifcrosscompiling;usuallyARM11orbettercores(VFPv2orbetterisalsosupported)GOARM=7:useVFPv3;usuallyCortex-AcoresIfindoubt,leavethisvariableunset,andadjustitifrequiredwhenyoufirstruntheGoexecutable.TheGoARMpageontheGocommunitywikicontainsfurtherdetailsregardingGo'sARMsupport.

環境變數中的$GOOS和$GOARCH是比較實用的兩個變數,可以用在不同平臺的交叉編譯中,只需要在go build之前設定這兩個變數即可,這也是go語言的優勢之一:可以編譯生成跨平臺執行的可執行檔案。感覺比QT更高效更輕量級,雖然生成的可執行檔案是大了一點,不過也在可接受的範圍之內。

例如,在Linux amd64架構下編譯Windows x86的可執行檔案,可以實用如下命令:

1 CGO_ENABLED=0GOOS=windowsGOARCH=386gobuildhello.go

遺憾的是交叉編譯暫不支援cgo方式,因此需要將環境變數$CGO_ENABLED設定為0,這樣執行之後會在當前目錄生成一個hello.exe的windows x86架構的可執行檔案: