Home > Backend Development > Golang > How do I build portable Go binaries for different operating systems and architectures?

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

DDD
Release: 2024-11-15 15:21:02
Original
482 people have browsed it

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
Copy after login

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
Copy after login

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

The above is the detailed content of How do I build portable Go binaries for different operating systems and architectures?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template