What does the w flag mean when passed in via the ldflags option to the go command?

You will get the smallest binaries if you compile with -ldflags '-w -s'.

The -w turns off DWARF debugging information: you will not be able to use gdb on the binary to look at specific functions or set breakpoints or get stack traces, because all the metadata gdb needs will not be included. You will also not be able to use other tools that depend on the information, like pprof profiling.

The -s turns off generation of the Go symbol table: you will not be able to use go tool nm to list the symbols in the binary. strip -s is like passing -s to -ldflags but it doesn’t strip quite as much. go tool nm might still work after strip -s. I am not completely sure.

None of these — not -ldflags -w, not -ldflags -s, not strip -s — should affect the execution of the actual program. They only affect whether you can debug or analyze the program with other tools.

Leave a Comment