Search code examples
gocompressionzlib

Golang's zlib is not compressing


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.


Solution

  • It seems that you are printing the wrong variable in last line. It should be fmt.Println(writer.Bytes())