Here's
a previous post where I talk about how to use Alpine Linux
to create static binaries
for Go
programs that use cgo
.
But that involves building in a docker
container(if you don't have a machine
with Alipne on it).
In this post I'll describe a easier way using zig cc
which has the advantage of
working directly on your machine.
wget https://ziglang.org/download/0.9.1/zig-linux-x86_64-0.9.1.tar.xz
tar xf zig-linux-x86_64-0.9.1.tar.xz
ln -s $(pwd)/zig-linux-x86_64-0.9.1/zig /usr/local/bin/
CC='zig cc -target x86_64-linux-musl' go build
This works out-of-the-box on Go 1.18, on older versions there may be some issues.
If x86_64
is not the CPU architecture you want, run zig targets | grep linux-musl
to see all supported architectures.