How to cross-compile a Go program on macOS?
Hi everyone! I'm writing Go on macOS and want to build a binary for Linux and Windows to run on a server and share with colleagues. I tried simply running `go build`, but it creates an executable only for macOS. I read that Go has built-in support for cross-compilation, but I can't figure out how to do it correctly.
Here's my simple code (main.go file):
```go
package main
import "fmt"
func main() {
fmt.Println("Hello, cross-compilation!")
}
```
I tried:
```bash
GOOS=linux GOARCH=amd64 go build main.go
```
But got an error: `cannot find package "main" in any of: ...` or a binary that doesn't run on Linux. What am I doing wrong? Do I need to install additional tools, like for CGo? I have macOS Big Sur, Go version 1.21. I'd be grateful.
No answers yet.
Log in to leave an answer
Blogs