centos安装upx
wget -c http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/ucl-1.03-2.el7.rf.x86_64.rpm
rpm -Uvh ucl-1.03-2.el7.rf.x86_64.rpm
yum install ucl
wget -c http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/upx-3.91-1.el7.rf.x86_64.rpm
rpm -Uvh upx-3.91-1.el7.rf.x86_64.rpm
yum install upx
#upx压缩golang可执行文件
首先加上编译参数-ldflags
$ go build -ldflags '-w -s'
$ ls -lh
-rwxr-xr-x 1 gangan staff 2.5M Aug 18 00:45 etcd-cli
-rw-r--r-- 1 gangan staff 456B Aug 18 00:34 main.go
可以发现小了近1MB
$ upx etcd-cli
# 此处省略压缩时的打印...
$ ls -lh
-rwxr-xr-x 1 gangan staff 897K Aug 18 00:49 etcd-cli
-rw-r--r-- 1 gangan staff 456B Aug 18 00:34 main.go
有疑问加站长微信联系(非本文作者)