Reducing Binary Size in Go: Strip Unnecessary Data with -ldflags "-w -s"

In today's world, where application performance and minimal resource consumption are top priorities, every byte counts. When building Go applications, the generated binary can sometimes be larger than what you might expect, especially when it contains debug information for the Go runtime and symbols for the linker. Fortunately, Go provides a handy way to reduce the binary size by stripping away unnecessary data. In this post, we'll dive into how to use the -ldflags "-w -s" option with go build to achieve this.

Understanding Go Binary Size

Before diving into the solution, it's essential to understand why Go binaries are large in the first place. One of the primary reasons is that Go binaries are statically linked by default. This means all the required libraries and dependencies are bundled into a single executable file, which ensures the binary is self-contained and can run without external dependencies. While this has advantages, it also leads to a bigger binary size.

Another significant contributor to the binary size is the debug information and symbols which are included by default. This information is helpful during the development phase, especially for debugging, but may not always be necessary in production.

Stripping Unnecessary Data

Enter -ldflags "-w -s".

The go build command allows the use of linker flags to modify the way the final binary is created. Two particularly useful flags for reducing binary size are:

  1. -w: This flag omits the DWARF symbol table, effectively removing debugging information.

  2. -s: This strips the symbol table and debug information from the binary.

By combining these flags, we can create a binary that is significantly smaller than its default counterpart.

To use these flags, modify your go build command as follows:

go build -ldflags "-w -s"

For example, if you are building a Go application named myapp, you would run:

go build -ldflags "-w -s" -o myapp

Caveats

While using these flags can provide a considerable reduction in binary size, there are some things to consider:

  1. Debugging: Stripping the debugging information means that tools like gdb will have a harder time debugging the application. So, it's a good practice to strip data only for production builds, while keeping debug information intact for development and testing.

  2. Error Reporting: Stripping the symbol table can make it harder to understand panic stack traces, as they won't have as much detailed information.

Conclusion

Reducing binary size is crucial, especially in constrained environments or when delivering applications to end-users where every byte counts. By using -ldflags "-w -s", you can significantly decrease the size of your Go binaries by stripping out unnecessary data. However, remember to keep the debugging information for builds used in testing or development, ensuring you have the necessary tools and data available for effective debugging and error reporting.

Previous
Previous

Graceful Shutdown in Go: Safeguarding Containerized Applications

Next
Next

Deploying Go Applications: Options and Best Practices