Standard library / compress/zlib

compress/zlib.Writer.Flush()

Push pending compressed data to the underlying writer without ending the ZLIB stream. Frequent flushes can increase size; Flush is not a substitute for Close. Expected stdout: 'true\nGo\n'.

Formal Syntax & Structure

go syntaxgo
func (z *Writer) Flush() error

Return Value

error

Nil on success; otherwise the operation error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "compress/zlib"  "fmt"  "io")
func must(e error) {  if e != nil {    panic(e)  }}func pack(s string) []byte {  var b bytes.Buffer  w := zlib.NewWriter(&b)  _, e := w.Write([]byte(s))  must(e)  must(w.Close())  return b.Bytes()}func unpack(b []byte) string {  r, e := zlib.NewReader(bytes.NewReader(b))  must(e)  p, e := io.ReadAll(r)  must(e)  must(r.Close())  return string(p)}func main() {  var b bytes.Buffer  w := zlib.NewWriter(&b)  _, e := w.Write([]byte("Go"))  must(e)  must(w.Flush())  fmt.Println(b.Len() > 0)  must(w.Close())  fmt.Println(unpack(b.Bytes()))}
Go Reference