To compile a Go program for multiple operating systems on a single machine, set environment variables GOOS and GOARCH accordingly.
GOOS (Go Operating System):
- set GOOS=windows
- set GOOS=linux
- set GOOS=darwin
GOARCH (Go Architecture):
- set GOARCH=386
- set GOARCH=amd64
More values here.
Example program:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
Build command on Windows for Linux 32-bit ELF file:
set GOOS=linux
set GOARCH=386
c:\Go\bin\go.exe build -o program.exe program.go
