When you use SQLite in your Go program, you are forced to use Cgo. While that works great, that breaks one of the nicest features of the Go toolchain: cross compilation.
Turns out there is actually a solution for this.
First, you need to install the
musl-cross package (and we're assuming you're running this on macOS).
musl-cross is a cross compilation toolchain.
brew install FiloSottile/musl-cross/musl-cross
Once you have this installed, you can compile for Linux like this:
CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ GOARCH=amd64 GOOS=linux CGO_ENABLED=1 \go build -ldflags "-linkmode external -extldflags -static"
By updating the
CXX environment variables to point to the
musl-cross toolchain, you can now cross compile with Cgo enabled.
You can also do the same when you want to cross compile to Windows. The instructions for that can be found here.