preface

Golang supports cross-compilation, which is generated on one platform and then executed on another. Moreover, the compilation tool [build] is built into Golang, which does not require you to download third-party packages, which is convenient for thieves.

cross-compilation

Compile on Mac, execute on Linux or Windows

# Linux to execute
CGO_ENABLED=0  GOOS=linux  GOARCH=amd64  go build main.go
# Windows go ahead and execute
CGO_ENABLED=0 GOOS=windows  GOARCH=amd64  go  build  main.go
Copy the code

Compile on Linux, execute on Mac or Windows

# Mac go down and execute
CGO_ENABLED=0 GOOS=darwin  GOARCH=amd64  go build main.go
Run the following command on Windows
CGO_ENABLED=0 GOOS=windows  GOARCH=amd64  go build main.go
Copy the code

Run it on Windows, and run it on Mac or Linux

You need to write a batch program and set it in there, because Terminal on Windows doesn’t support shell, which is a little different from Mac and Linux

Execute under Mac
SET  CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build main.go
Copy the code
# Linux to execute
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build main.go
Copy the code

Parameters that

  • CGO_ENABLED: CGO indicates the tool in Golang. CGO_ENABLED indicates that CGO is disabled. CGO cannot be used in cross-compilation
  • GOOS: Target platform
    • MAC corresponds to Darwin
    • Linux corresponding Linux
    • Windows corresponding Windows
  • GOARCH: The architecture of the target platform [386, AMD64, ARM], currently personal computers on the market are generally based on AMD64 architecture
    • 386, also known as x86, corresponds to a 32-bit operating system
    • Amd64 also known as X64 corresponds to a 64-bit operating system
    • The ARM architecture is commonly used for embedded development. Android, IOS, Win Mobile, TIZEN, etc

Why do you need to set the parameters in front of go Build on Linux or Mac?

Go env lists our golang default environment variables, which can be changed by setting variables in the shell when we only want to change their environment variables once.

case

/ / the default >#go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/data/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build171002917=/tmp/go-build -gno-record-gcc-switches"
>#/ / change >#CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go env
GOARCH="amd64"  # changed
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=".exe"
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="windows" # changed
GOPATH="/data/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"  # changed
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-m64 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build668031908=/tmp/go-build -gno-record-gcc-switches"
>#// After change >#go env
GOARCH="amd64" # reducing the
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"      # the default value
GOPATH="/data/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build240557259=/tmp/go-build -gno-record-gcc-switches"
>#
Copy the code