1 回答

TA贡献1821条经验 获得超4个赞
你没有Close()
,encoder
所以它不会刷新所有数据。来自文档(强调我的):
func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser
NewEncoder 返回一个新的 base64 流编码器。写入返回的 writer 的数据将使用 enc 进行编码,然后写入 w。Base64 编码以 4 字节块运行;完成写入后,调用者必须关闭返回的编码器以刷新任何部分写入的块。
我还引用了文档中的示例,其中有一个很好的评论:
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 关注
- 121 浏览
添加回答
举报