英文:
Golang's zlib is not compressing
问题
我有以下一段代码,它将一个十六进制字符串转换为字节数组;但是当我尝试使用zlib压缩这个字节数组时,它不起作用(我在压缩前后打印了数据,但是它们是相同的):
data, err := hex.DecodeString(request.Log)
if err != nil {
panic(err)
}
fmt.Println(data)
var writer bytes.Buffer
gz := zlib.NewWriter(&writer)
if _, err = gz.Write(data); err != nil {
println("error: ", err)
return
}
gz.Flush()
if err = gz.Close(); err != nil {
println("err", err)
return
}
fmt.Println(data)
我还尝试在关闭之前手动刷新写入器,但没有帮助。
英文:
I have the following piece of code that takes a hex string and converts it into byte array; But when I try to compress the mentioned byte array using zlib it doesn't work(I print the data before and after compression but there are the same):
data, err := hex.DecodeString(request.Log)
if err != nil {
panic(err)
}
fmt.Println(data)
var writer bytes.Buffer
gz := zlib.NewWriter(&writer)
if _, err = gz.Write(data); err != nil {
println("error: ", err)
return
}
gz.Flush()
if err = gz.Close(); err != nil {
println("err", err)
return
}
fmt.Println(data)
I also tried to manually flush the writer before closing it but it didn't help.
答案1
得分: 2
似乎你在最后一行打印了错误的变量。应该是fmt.Println(writer.Bytes())
。
英文:
It seems that you are printing the wrong variable in last line. It should be fmt.Println(writer.Bytes())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论