How to cross-compile Go programs for Windows, macOS, and Linux

You can use the go env command to view the values of these variables on your machine:

$ go env
...
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
...

Compile for Windows

Here’s the command you need to run to compile your Go project for a 64-bit Windows machine:

$ GOOS=windows GOARCH=amd64 go build -o bin/app-amd64.exe app.go

In this scenario, GOOS is windows, and GOARCH is amd64 indicating a 64-bit architecture. If you need to support a 32-bit architecture, all you need to do is change GOARCH to 386.

$ GOOS=windows GOARCH=386 go build -o bin/app-386.exe app.go

Compile for macOS

The GOARCH values for Windows are also valid for macOS, but in this case the required GOOS value is darwin:

# 64-bit
$ GOOS=darwin GOARCH=amd64 go build -o bin/app-amd64-darwin app.go

# 32-bit
$ GOOS=darwin GOARCH=386 go build -o bin/app-386-darwin app.go

Compile for Linux

To build your Go program for Linux, use linux as the value of GOOS and the appropriate GOARCH value for your target CPU architecture:

# 64-bit
$ GOOS=linux GOARCH=amd64 go build -o bin/app-amd64-linux app.go

# 32-bit
$ GOOS=linux GOARCH=386 go build -o bin/app-386-linux app.go

 

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Можно использовать следующие HTML-теги и атрибуты: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>