我有一段代碼,它是一個字符串,然后將壓縮版本讀回原始版本。如何測量壓縮級別?即計算字符串前后的字節數? sentence := "A long time ago in a galaxy far, far away..." var buf bytes.Buffer zw := gzip.NewWriter(&buf) // Setting the Header fields is optional. zw.Name = "a-new-hope.txt" zw.Comment = "an epic space opera by George Lucas" zw.ModTime = time.Date(1977, time.May, 25, 0, 0, 0, 0, time.UTC) _, err := zw.Write([]byte(sentence)) if err != nil { log.Fatal(err) } if err := zw.Close(); err != nil { log.Fatal(err) }我在 go playground 中有這段代碼https://play.golang.org/p/9T2BOpCFmar
1 回答

弒天下
TA貢獻1818條經驗 獲得超8個贊
尺碼前:
fmt.Println(len(sentence))
尺寸后:
fmt.Println(buf.Len())
一定要在關閉編寫器之前調用 buf.Len()
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報
0/150
提交
取消