How to build a release version binary in Go?

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?

56665 次浏览

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.

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