Skip to content

Golang 二进制包体积压缩

命令

bash
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/service-api cmd/server/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/service-api-2 cmd/server/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/service-api-3 cmd/server/main.go && upx -9 bin/service-api-3
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/service-api-4 cmd/server/main.go && upx -9 bin/service-api-4
ls -lah bin/service-api*

-rwxr-xr-x 1 root root  41M Jul 23 11:06 bin/service-api
-rwxr-xr-x 1 root root  30M Jul 23 11:06 bin/service-api-2
-rwxr-xr-x 1 root root 9.7M Jul 23 11:07 bin/service-api-3
-rwxr-xr-x 1 root root  18M Jul 23 11:08 bin/service-api-4