常规打包方法

$ go build
$ ls -lh
-rwxr-xr-x. 1 root root  31M Aug 18 22:53 gokins-sqlite

压缩可执行文件

  • 首先加上编译参数-ldflags '-w -s'

    [root@gokins gokins]# CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags '-w -s' -o gokins-sqlite main.go
    [root@gokins gokins]# ls -lh gokins-sqlite
    -rwxr-xr-x. 1 root root 23M Aug 18 23:05 gokins-sqlite
    
  • 使用upx压缩,Linux、Mac和Win都有,这里以Linux为例

    [root@gokins gokins]# upx gokins-sqlite -o gokins-sqlite_compress
                           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
       --------------------   ------   -----------   -----------
      23065992 ->   9815872   42.56%   linux/amd64   gokins-sqlite_compress     
    
    Packed 1 file.
    

补充