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?
Asked
Active
Viewed 6.8k times
2 Answers
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"
.
-
2There's some documentation on this here - https://golang.org/doc/gdb#Introduction. – Greg Apr 13 '15 at 07:44
-
4You'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
-
4I 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
-
2It would appear that -s doesn't affect GOOS=darwin – Chris B. Apr 25 '17 at 14:05
-
1Only startup-time changes. – Burak Büyükatlı Jul 31 '17 at 09:29
-
2Just 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