This is a creation in Article, where the information may have evolved or changed.
Gox Getting Started tutorial
What is cross-compiling first?
Cross-compiling means that you can compile programs on Linux that can run on Windows and compile 64-bit systems on 32-bit systems.
Gox is a tool that allows you to use Golang cross-compilation.
Installing Gox
First, you need to have Golang installed on your machine. Goroot,gopath These two environment variables are configured.
The configuration on my machine is like this (for reference only)
Exportgoroot=$HOME/goExportGopath=$HOME/goprojExportgobin=$GOPATH/binExportPath=$PATH:$GOBIN
Installation Gox is actually very simple (only 2 steps).
Go get github.com /mitchellh/gox
After the command line input gox -h
, you should use the output, otherwise you have to check the path variable set is correct.
The next step is to compile the libraries needed for other platforms. This step is a bit slow, be a bit patient.
gox -build-toolchain
The output is probably like this.
The toolchain build can ' t be parallelized because compiling a Singlego source directory can is only is done forOne platform atA Time. Therefore, theToolchain forEach platform'll be built one atA Time.- toolchain:darwin/386- Toolchain:darwin/amd64...
When this step is complete, the Gox is ready to start using.
Using Gox (easy to get started)
Let's take a look at the power of Gox.
It is important to note that Gox cannot specify a file to compile.
For the sake of convenience, we first go to the $GOPATH/src
next, create a Hello folder. Write a hello.go program. Like what
package mainfunc main () {println (" Hello World ")}
Go to the program directory and run Gox directly. The program generates 17 files in one breath. Across WINDOWS,LINUX,MAC,FREEBSD,NETBSD five operating systems. and 3 different processors (386, AMD64, ARM)
A description of the processor can look at this http://www.361way.com/cpuinfo/1510.html
ARM type of processor, on the phone with more.
Using Gox (Specify the built platform (OS) and Processor (ARCH))
Many of the options in fact gox -h
help can be found very clearly.
If we want to build Linux and programs on Windows, just pass the command:
gox-os " Windows Linux "-arch amd64
directory, you can see the two programs that were generated.
hello_linux_amd64hello_windows_amd64.exe
gox"windows/amd64 linux/amd64"
Advanced
You can also continue to learn something goxc, the tool encapsulates the Gox provides more powerful functionality.
Problems that you may encounter
- cross-compilation does not yet support CGO (estimates will not be supported for another 1 years)