這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
本地開發環境是os x 伺服器是linux,所以需要編譯linux用的go程式,方法如下:
各平台的GOOS和GOARCH參考
OS ARCH OS versionv
linux 386 / amd64 / arm >= Linux 2.6
darwin 386 / amd64 OS X (Snow Leopard + Lion)
freebsd 386 / amd64 >= FreeBSD 7
windows 386 / amd64 >= Windows 2000
$ cd /usr/local/go/src$ sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
這裡並不是重新編譯Go,因為安裝Go的時候,只是編譯了本地系統需要的東西;而需要跨平台交叉編譯,需要在Go中增加對其他平台的支援。所以,有 ./make.bash 這麼一個過程。
執行結果類似如下:
sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bashPassword:Building C bootstrap tool.cmd/distBuilding compilers and Go bootstrap tool for host, darwin/amd64.lib9libbiolibmachmisc/pprofcmd/addr2linecmd/covcmd/nmcmd/objdumpcmd/packcmd/profcmd/cc……pkg/text/template/parsepkg/text/templatepkg/go/docpkg/go/buildcmd/gopkg/runtime (linux/amd64)Building packages and commands for host, darwin/amd64.runtimeerrorssync/atomicunicodeunicode/utf8mathsyncunicode/utf16crypto/subtleiosyscall……….net/rpc/jsonrpctesting/iotesttesting/quickBuilding packages and commands for linux/amd64.runtimeerrorssync/atomicunicodeunicode/utf8mathsyncunicode/utf16……..testingnet/rpc/jsonrpctesting/iotesttesting/quickInstalled Go for linux/amd64 in /usr/local/goInstalled commands in /usr/local/go/bin
到原始碼目錄下執行:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build sina.go
不帶前面參數的 go build 只是編譯出開發環境適用的執行檔案。