This is a creation in Article, where the information may have evolved or changed.
From the first day to learn Golang know that it has a very exciting feature is cross-platform compilation, but has not been used. Recently made a small thing need to use this feature, so the search on the Internet.
First find is the domestic article is the use of download go source package, will be compiled and installed, complete after go/src make.bash generation cross-platform compiler, so that in each time you need to specify GOOS and Goarch to compile, such as:
Cgo_enabled=0 goos=linux goarch=amd64 Go build//compile to Linux 64-bit system program
If you need to compile a program under Windows 64-bit, write another command:
Cgo_enabled=0 goos=windows goarch=amd64 Go build//compile as a program under Windows 64-bit system
See: "Golang Cross-platform compiler program"
This approach is useful for one-time compilation, testing, and learning, but it can be problematic for a one-time processing of multi-platform compilations
First you need to run the appropriate make.bash on the compiler for each platform you want to deploy to generate the required compilers
Execute the corresponding compilation command each time (this is a bit farfetched, because the shell can also be used to implement batch processing)
This machine needs to keep all the generated compilers, and if other people need them, then you need to do the build work of the required compilers again.
In order to keep the machine clean (hehe)
Well, except that the first one is a bit of a hassle, the rest is not a problem, the main goal is to elicit the second approach, which is to use Docker to deploy the build:
First you need to install Docker
Download the cross-platform compilation version of Go, I use Golang 1.4.2-cross
sudo docker pull Golang:1.4.2cross
Goarch= "AMD64" gobin= "" gochar= "6" goexe= "" gohostarch= "AMD64" gohostos= "Linux" goos= "Linux" gopath= "/go" gorace= "" goroot= "/usr/src/go" gotooldir= "/usr/src/go/pkg/tool/linux_amd64" cc= "gcc" gogccflags= "-fpic-m64-pthread- Fmessage-length=0 "cxx=" g++ "cgo_enabled=" 1 "
root@55140be4a751:/usr/src/go/bin#
among them:"$PWD"refers to a data volume that mounts the current directory as Docker
The first /go/src/myapp represents the name of the data volume to mount to the current directory
The second /go/src/myapp represents a working directory that is entered directly after the shell is
Docker run--rm-it-v "$PWD":/usr/src/myapp-w/usr/src/myapp Golang:1.3-cross bash $ for GOOS in Windows Linux; Do > for goarch in386 amd64; Do > Go build-v-o myapp-$GOOS-$GOARCH//Note there are no spaces between the minus sign > Done > did
GOOS after Windows Linux refers to the compilation of why the platform, the use of that then input that, after the run to enter the corresponding results:
Main.go myapp-linux-386 myapp-linux-amd64 myapp-windows-386 myapp-windows-amd64