Golang supports cross-compilation for generating executable programs on one platform for another platform.

1. Mac

Compile 64-bit executables for Linux, Windows platforms under Mac.

1
2
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build test.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

2. Linux

Compiling 64-bit executable programs for Mac, Windows platforms under Linux.

1
2
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build test.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

3. Windows

Compiling 64-bit executable programs for Mac, Linux platforms under Windows.

1
2
3
4
SET CGO_ENABLED=0
SET GOOS=darwin3
SET GOARCH=amd64
go build test.go
1
2
3
4
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build test.go
  • GOOS: target executable running operating system, supports darwin, freebsd, linux, windows.
  • GOARCH: target executable operating system architecture, including 386, amd64, arm.

Before Golang 1.5, you need to configure the cross-compilation environment when you cross-compile for the first time.

1
2
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash