Golang Cross-compiling tool, getting started with Gox

Source: Internet
Author: User
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)

Export goroot= $HOME/goexport gopath= $HOME/goprojexport gobin= $GOPATH/binexport path= $PATH: $GOBIN

Installing the Gox is actually very simple (only 2 steps required).

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 for one PLATF ORM at a time. Therefore,the Toolchain for each platform'll be built one at a 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

can also be used this way, the effect is equivalent to the command just now

Gox-osarch "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

    1. Cross-compilation does not support CGO for the time being (estimates will not be supported for another 1 years)

Related Resources

    • Cross-platform compilation Go program
    • How to use Golang cross-compilation
    • Golang Online Compilation
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.