首頁 > 後端開發 > Golang > 如何為不同的作業系統和架構建立可移植的 Go 二進位檔案?

如何為不同的作業系統和架構建立可移植的 Go 二進位檔案?

DDD
發布: 2024-11-15 15:21:02
原創
510 人瀏覽過

How do I build portable Go binaries for different operating systems and architectures?

Portability of Go Binaries

Go binaries are designed to be cross-platform, allowing developers to build applications that can run on multiple operating systems and architectures. However, achieving portability across different configurations requires an understanding of Go's linking and compilation process.

Q1: Portability of amd64 Linux Binaries

A: Yes, an amd64 binary compiled on an Ubuntu system will run on any other 64-bit Ubuntu/Debian system without modification. This is because Linux uses a common set of system libraries for 64-bit applications, ensuring binary compatibility across distributions.

Q2: Building 32-bit Binaries for Debianlikes

A: To create an x86_64 binary that will run on both 64-bit and 32-bit Debianlikes, use the following command:

GOOS=windows GOARCH=386 go build
登入後複製

Setting GOOS to "windows" and GOARCH to "386" forces the build process to generate a 32-bit executable.

Q3: Ensuring x86_64 Executables on Windows

A: Building a binary on a 64-bit Windows system doesn't automatically guarantee that it will target the x86_64 architecture. To ensure an x86_64 executable, specify the -target flag when building:

go build -target=amd64
登入後複製

This flag explicitly sets the target architecture for the compiled binary, regardless of the host operating system.

以上是如何為不同的作業系統和架構建立可移植的 Go 二進位檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板