In C, we can build a debug version or a release version of the binary files (the object files and the executable). How can we do this in Go?
4 Answers
In Go, it isn't typical to have a debug version or a release version.
By default, go build combines symbol and debug info with binary files. However, you can remove the symbol and debug info with go build -ldflags "-s -w".
9 Comments
The accepted and high-rated answer doesn't work for me. While binary files generated by go build do include debug symbols, they are compiled with optimization enabled, which makes it almost impossible to debug with delve.
The following option works for me (I find it in delve document):
go build -gcflags="all=-N -l"
Comments
As stated earlier, there is no such thing as a "debug" or "release" binaries in Go.
However, there is a specific build environment variable called CGO_ENABLED. Which is enabled by default. And will most likely make your binary larger.
However when you deploy your binary for a scratch Docker image consider setting CGO_ENABLED=0 - as no host OS needs to be bundled.
Unless you use packages that contains C code, which in that case CGO_ENABLED need to be set to 1.
Also there is a GOOS environment variable, which you might want to set explicitly to for example linux.