Skip to content

Reduce binary file size in Golang


When compiling Golang code, the resulting binary file can sometimes be quite large. This can be a problem when distributing the code or deploying it to production servers. In this blog post, we’ll explore some techniques for reducing the size of Golang binary files.

Compile with parameter

By using the -s and -w flags during compilation, you can strip out unnecessary information from the binary file. The -s flag removes all symbol table and debugging information, while the -w flag removes DWARF debugging information. This can significantly reduce the size of the binary file.

go build -ldflags="-w -s"
Parameter Meaning
-w Remove DWARF debug information, which will result in no file name or line number information in the call stack when a panic occurs.
-s Remove symbol table information, which cannot be debugged with gdb.

upx tool

You can also use external tools to further reduce the size of Golang binary files. For example, UPX (the Ultimate Packer for eXecutables) is a popular tool for compressing binary files. UPX can compress Golang binary files by up to 60%, resulting in much smaller file sizes.

To use UPX, simply download the binary for your operating system and run it on your Golang binary file. UPX will compress the binary file and create a new, smaller binary file. This new binary file can be used in production, reducing the size of your application.

upx devapp

Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2020
UPX 3.96        Markus Oberhumer, Laszlo Molnar & John Reiser   Jan 23rd 2020

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
  14229578 ->   7080260   49.76%   linux/amd64   devapp

Packed 1 file.
Leave a message

  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback, replace # with @