Reduce the File Size of the Golang Build Compiled Package

Updated at: 2020-04-23 Views: 687 Words: 146 Category: golang

After using Golang Gin wrote the backend program with GoAdmin, the volume of the distribution package generated by go build has reached a whopping 30M. Upload Ali Cloud Server, which has a transfer speed of 400K/s for 1 minute.

30075408

Find a solution online, plus compilation parameters.

go build -ldflags "-s-w"

The volume has been reduced to 23M.

22778576

File size drops by about a quarter.

Compilation parameters

  • The function of -s is to remove symbolic information. If you remove the symbol table, the stack trace will not see the file name and the wrong line number information when you golang panic.
  • The purpose of -w is to remove DWARF tables debugging information. The result is that the program you get won't debug with GDB.

DWARF tables

DWARF tables needed for debuggers

More extreme compression methods

https://blog.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick/

Leave a Comment