Cross-compilation
Cross-compilation is the process of generating a binary executable file for a different architecture than the one on which we are working without having access to other machines. The main benefit that we receive from cross-compilation is that we do not need a second or third machine to create and distribute executable files for different architectures. This means that we basically need just a single machine for our development. Fortunately, Go has built-in support for cross-compilation.
To cross-compile a Go source file, we need to set the GOOS
and GOARCH
environment variables to the target operating system and architecture, respectively, which is not as difficult as it sounds.
You can find a list of available values for the GOOS
and GOARCH
environment variables at https://golang.org/doc/install/source. Keep in mind, however, that not all GOOS
and GOARCH
combinations are valid.
The code of crossCompile.go
is the following:
package main...