91

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?

BIBD
  • 15,107
  • 25
  • 85
  • 137
catric mia
  • 1,137
  • 2
  • 9
  • 8

2 Answers2

144

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

Greg
  • 3,731
  • 1
  • 29
  • 25
hiwjd0
  • 1,889
  • 1
  • 14
  • 7
  • 2
    There's some documentation on this here - https://golang.org/doc/gdb#Introduction. – Greg Apr 13 '15 at 07:44
  • 4
    You're right, but I think there's a good reason it's not typical to strip symbols--if you get a report of a panic out in the wild, for example, it'd be great to have the symbols there for an informative stacktrace. – twotwotwo Apr 13 '15 at 07:56
  • 4
    I think "-s" (omit symbol table and debug info) already includes "-w" (omit DWARF symbol table), so there should be no need to specify both. With the executable I tried this on, "-s -w" gives exactly the same size as "-s" alone. – rob74 Apr 13 '15 at 08:20
  • 2
    It would appear that -s doesn't affect GOOS=darwin – Chris B. Apr 25 '17 at 14:05
  • 1
    Only startup-time changes. – Burak Büyükatlı Jul 31 '17 at 09:29
  • 2
    Just made some experiments on my Mac. Results: -s does not imply -w, binary size with/without -s is the same. -w reduces binary from 12 to 8MB. ALSO: there's NO difference in stack-traces between "go build" and "-s -w" builds. Of course it's just for my software, probably there are corner cases. – Aleksandr Kravets May 23 '18 at 07:52
  • The -s flag removes all symbols (including the DWARF symbol table, which -w removes.) So you only need -s, I think. – luis.espinal Feb 05 '22 at 20:38
27

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.

rob74
  • 4,939
  • 29
  • 31