Cross-platform development with Go
Cross-platform development with Go is a breeze. You can write code running on various operating systems and architectures with ease.
Cross-platform development with Go can be achieved by using the GOOS
and GOARCH
environment variables. The GOOS
environment variable specifies the OS you want to target, and the GOARCH
environment variable specifies your target architecture.
For example, assume you have a Go source file named main.go
:
package main import "fmt" func main() { fmt.Println("This program runs in any OS!") }
To compile code for Linux, you would set the GOOS
environment variable to linux
and the GOARCH
environment variable to amd64
:
GOOS=linux GOARCH=amd64 go build
This command will compile the code for Linux.
You can also use the GOOS
and GOARCH
environment variables to run code on different platforms. For example, to run the code you compiled for Linux on macOS, you would set...