语音解读
图文解释
golang中跨平台交叉编译涉及两个重要的环境变量:GOOS和GOARCH,分别代表Target Host OS和Target Host ARCH,如果没有显式设置这些环境变量,我们通过go env可以看到go编译器眼中这两个环境变量的当前值:
go env GOARCH="amd64" GOBIN="/home/work/software/go/bin" GOEXE="" GOHOSTARCH="amd64" //主机arch GOHOSTOS="linux" //主机host GOOS="linux" GOPATH="/home/work/software/go/src/pkg:/home/work/develop/git/content_filter:/home/work/develop/git/content_filter/.koala" GORACE="" GOROOT="/home/work/software/go" GOTOOLDIR="/home/work/software/go/pkg/tool/linux_amd64" GO15vendOREXPERIMENT="1" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0" CXX="g++" CGO_ENABLED="1"
如果想要跨平台编译go程序,只需在build的时候制定目标主机的os和arch,如下例子所示所示
GOOS=windows GOARCH=386 go build -o hello.exe hello.go
其中二者可以设置如下参数:
$GOOS $GOARCH darwin 386 darwin amd64 freebsd 386 freebsd amd64 linux 386 linux amd64 linux arm incomplete windows 386 incomplete
如果想要编译其他平台的可执行脚本,秩序指定相应的os和arch即可。