kota's memex

sub-package

Compile a sub-package. Lots of go programs are structured such that the repo is a library and has a cmd file with one of more programs using the library.
go build -o thing ./cmd/thing

go static compile

By default, go programs are not-quite statically compiled which is sometimes an issue when using a pre-compiled binary on a very old linux system.

cross compile

GOOS=windows GOARCH=amd64 go build -o=/tmp/windows_amd64/foo.exe .

List all supported OS/architechture combinations:

$ go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/386
darwin/amd64
...

disable compiler optimizations

go build -gcflags="all=-N -l" -o=/tmp/foo .

minimize binary size

go build -ldflags="-s -w" -o=/tmp/foo .

If you need really small binaries check out this post:
https://blog.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick/

file not found error

In some cases go will build binaries with dynamic linking rather than static. This happens when using cgo and certain network features. If you're just missing a library on your local system you get a nice helpful error message like:
(libfoo.so.3: cannot open shared object file: No such file or directory)

However, if you happen to be missing the dynamic linker itself you'll get an error that just says: no such file or directory. This happens most commonly on alpine. It'll have mostly the same kernel, but perhaps a different linker from the system the software was built on.

If you're not actually using cgo the fix is easy, just set: CGO_ENABLED=0 and build the program again. If you are using cgo I think you need to actually build on your target system (ie. alpine).