Home  >  Article  >  Backend Development  >  A brief introduction to Golang cross-compilation

A brief introduction to Golang cross-compilation

尚
forward
2020-01-02 17:08:392949browse

A brief introduction to Golang cross-compilation

Golang supports cross-compilation, generating an executable program on one platform for another platform. This tutorial introduces you to cross-compilation of golang.

1. What is cross-compilation

Simply put, it is to generate executable code on one platform on another platform. The same architecture can run different operating systems; similarly, the same operating system can also run on different architectures.

2. Preparation before cross-compilation

This article only introduces Golang cross-compilation under Windows platform

Installing TDM-GCC

Download address: http://tdm-gcc.tdragon.net/download

After the installation is completed, add the bin folder in TDM-GCC to the environment variable.

3. Brief description of cross-compilation

A brief introduction to Golang cross-compilation

In fact, just save the above picture. You need to remember three parameters, GOOS (Go’s target system) , GOARCH (GO's target architecture).

After the setting is completed, view the current configuration through go env.

Note: In different command prompts (CMD) or close and reopen the command prompt, the set GOOS and GOARCH are not interoperable. In other words, GOOS and GOARCH can be understood as local variables and will not be saved. By default, Golang builds the current system and architecture every time.

4. Practical practice

1. Default compilation

A brief introduction to Golang cross-compilation

Currently, a Windows is compiled using the default configuration. For the exe executable file under the platform, we also set GOOS and GOARCH before compiling it.

2. Cross-compile an Arm Linux

A brief introduction to Golang cross-compilation

The SET command seems to be invalid in Windows PowerShell. I will investigate the reason in detail when I have time. , so CMD is used in this step.
After setting up, just run go bulid again, and you will get a Linux executable file without suffix.

A brief introduction to Golang cross-compilation

For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.

The above is the detailed content of A brief introduction to Golang cross-compilation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete