Reputation: 1247
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?
Upvotes: 102
Views: 91392
Reputation: 1680
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"
Upvotes: 1
Reputation: 3175
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
.
Upvotes: 0
Reputation: 5238
You can instruct the linker to strip debug symbols by using
go install -ldflags '-s'
I just tried it on a fairly large executable (one of the GXUI samples), and this reduced it from ~16M to ~10M. As always, your mileage may vary...
Here is a full list of all linker options.
Upvotes: 30
Reputation: 2209
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"
.
Upvotes: 162