Go cross-platform compilation method learning

Source: Internet
Author: User
Tags docker run learn golang

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
    • Enter and run once go env determine a compilation environment
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"
    • Go to the Bin directory under the root directory we can see all the compiled platforms, is not a lot:)
ls darwin_386    dragonfly_386    
freebsd_386 freebsd_arm gofmt
Linux_arm nacl_amd64p32 netbsd_386 netbsd_arm
Openbsd_amd64 plan9_amd64 windows_386darwin_amd64 dragonfly_amd64 freebsd_amd64 Go
Linux_386 nacl_386
Nacl_arm netbsd_amd64 openbsd_386
plan9_386 solaris_amd64 windows_amd64
  • Run:Docker Run--rm-it-v"$PWD":/go/src/myapp-w/go/src/myapp Golang:1.4.2-cross Bash

where: " $PWD

first one /go/src/myapp represents the name of the data volume to mount to the current directory

a second /go/src/myapp represents a working directory that is entered directly after the shell

    • run in the shell that starts:
Docker run-rm"$PWD":/usr/src/myapp-w /usr/src/myapp Golang:1.3 - for on do > go build-v-o myapp-$GOOS-    Done

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

Go cross-platform compilation method learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.