How to compress Go executable file with UPX

藏色散人
Release: 2021-12-07 16:01:30
forward
3274 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

After compression, the file size becomes much smaller.

Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/14/2019 9:52 AM 495616 main.exe
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!