1 回答

TA貢獻1821條經(jīng)驗 獲得超5個贊
你沒有Close()
,encoder
所以它不會刷新所有數(shù)據(jù)。來自文檔(強調(diào)我的):
func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser
NewEncoder 返回一個新的 base64 流編碼器。寫入返回的 writer 的數(shù)據(jù)將使用 enc 進行編碼,然后寫入 w。Base64 編碼以 4 字節(jié)塊運行;完成寫入后,調(diào)用者必須關閉返回的編碼器以刷新任何部分寫入的塊。
我還引用了文檔中的示例,其中有一個很好的評論:
package main
import (
"encoding/base64"
"os"
)
func main() {
input := []byte("foo\x00bar")
encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
encoder.Write(input)
// Must close the encoder when finished to flush any partial blocks.
// If you comment out the following line, the last partial block "r"
// won't be encoded.
encoder.Close()
}
- 1 回答
- 0 關注
- 157 瀏覽
添加回答
舉報