How to build a release version binary in Go?

CGoBuildRelease

C Problem Overview


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?

C Solutions


Solution 1 - C

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".

Solution 2 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncatric miaView Question on Stackoverflow
Solution 1 - Chiwjd0View Answer on Stackoverflow
Solution 2 - Crob74View Answer on Stackoverflow