Go CrossCompile
Crosscompile under GoLang
Python is cool and everybody like it, but i also like the Concept of writing some Code, compile it for different Platforms and run it everywhere. Google’s Go Language got the possiblity to compile it for multiple Architectures and Operating Systems at the same time. Why not give a try … ?
Little Hello World
package main
import (
"fmt"
"os"
)
func main() {
s := "world"
if len(os.Args) > 1 {
s = os.Args[1]
}
fmt.Printf("Hello, %v!", s)
fmt.Println("")
if s == "fail" {
os.Exit(30)
}
}
go.mod
module example.com/test
go 1.18
Compile and run under macOS
go build
./test
Hello, world!
CrossCompile Script
#!/usr/bin/env bash
archs=(amd64 arm64)
os=(darwin freebsd linux openbsd windows)
name="hello"
for arch in ${archs[@]}; do
for os in ${os[@]}; do
env GOOS=${os} GOARCH=${arch} go build -o ${name}_${os}-${arch}
done
done
Compile it
execute it …