This article is introduced by the tutorial column ofgolangon how to use UPX to compress Go executable files. I hope it will be helpful to friends in need!
Usually the executable built withgois a bit large, but it is always desirable to produce a smaller executable.
In this article, we will introduce several methods to reduce the size of executable files.
The net effect is that the size of the executable file will be much smaller than normally generated.
Usually built files have the following sizes.
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/14/2019 9:47 AM 1974272 main-ori.exe
Add build flags
You can add two ld parameters when using the go tool to build the project, they are -s and -w.
go build -ldflags="-s -w" main.go
-s: Omit symbol table and debugging information. In most cases, they are not needed in a production environment.
-w: Omit DWARF messages.
These two parameters will not affect the execution of the program, but it will reduce the size of the executable file.
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/14/2019 9:48 AM 1427968 main-flag.exe
UPX Compression
upx is a binary compression tool. It can be used to compress binary files and further reduce file size.
The command to compress a file is:
upx main.exe
After compression, the file size becomes much smaller.
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/14/2019 9:52 AM 495616 main.exe
Happy coding.
Original address: https://www.pixelstech.net/article/1576288399-GoLang-to-build-smaller-executable-file
Translation address: https:// learnku.com/go/t/63386
The above is the detailed content of How to compress Go executable file with UPX. For more information, please follow other related articles on the PHP Chinese website!