Quantcast
Channel: 小惡魔 – 電腦技術 – 工作筆記 – AppleBOY
Viewing all articles
Browse latest Browse all 325

用 upx 壓縮 Go 語言執行擋

$
0
0
Go-brown-side.sh 剛開始學 Go 語言的時候,跟學習其他語言一樣,寫了底下一個簡單的 Hello World 檔案
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
}
這是一個最簡單的程式碼,接著透過 go build 的方式編譯出執行檔,這時候我們看看檔案大小:
$ du -sh hello*
1.8M    hello
4.0K    hello.go
一個 4K 大小的 Hello World,竟然要 1.8MB (使用 Go 1.9 版本),雖然說現在硬碟很便宜,網路傳輸也很快,但是對於大量 Deploy 到多台機器時,還是需要考量檔案大小,當然是越小越好,這時候我們可以使用 Go 語言的 shared 方式編譯出共同的 .a 檔案,將此檔案丟到全部機器,這樣再去編譯主執行擋,可以發現 Size 變成很小,應該不會超過 20K 吧。但是此種方式比較少人使用,大部分還直接將主程式編譯出來,這時來介紹另外一個工具用來減少 Binary 大小,叫做 UPX
UPX – the Ultimate Packer for eXecutables
當然 upx 不只是可以壓縮 Go 的編譯檔案,其他編譯檔案也可以壓縮喔,底下是透過 upx 使用兩種不同的編譯方式來減少執行檔大小
$ du -sh hello*
1.8M    hello
# upx -o hello_1 hello
636K    hello_1
# upx --brute -o hello_2 hello
492K    hello_2
4.0K    hello.go
用了兩種方式來壓縮大小,越小的檔案,處理的時間越久,壓縮比例至少超過 70%,效果相當不錯,用 Hello World 沒啥感覺,實際拿個 Produciton 的案子來試試看,就拿 Gorush 來壓縮看看
$ du -sh *
15M     gorush
# upx --brute -o gorush_1 gorush
3.4M    gorush_1
# upx -o gorush_2 gorush
4.7M    gorush_2
壓縮比例也差不多 70 %,但是使用 --brute 處理時間蠻久的,如果要加速部署,用一般模式即可。別因為些微差異,就花費多餘時間在壓縮上。

Viewing all articles
Browse latest Browse all 325

Trending Articles


Girasoles para colorear


mayabang Quotes, Torpe Quotes, tanga Quotes


Tagalog Quotes About Crush – Tagalog Love Quotes


OFW quotes : Pinoy Tagalog Quotes


Long Distance Relationship Tagalog Love Quotes


Tagalog Quotes To Move on and More Love Love Love Quotes


5 Tagalog Relationship Rules


Best Crush Tagalog Quotes And Sayings 2017


Re:Mutton Pies (lleechef)


FORECLOSURE OF REAL ESTATE MORTGAGE


Sapos para colorear


tagalog love Quotes – Tiwala Quotes


Break up Quotes Tagalog Love Quote – Broken Hearted Quotes Tagalog


Patama Quotes : Tagalog Inspirational Quotes


Pamatay na Banat and Mga Patama Love Quotes


Tagalog Long Distance Relationship Love Quotes


BARKADA TAGALOG QUOTES


“BAHAY KUBO HUGOT”


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 10.7.1 by Vimeo.com, Inc.