Standard library / compress/bzip2

compress/bzip2.NewReader()

Create a reader that decompresses BZIP2 input. Go provides no BZIP2 writer in this package; errors are reported by Read. The example uses a fixed two-byte payload compressed by Python bz2. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func NewReader(r io.Reader) io.Reader

Parameters & Attributes

NameTypeDescription
rio.ReaderCompressed input stream. Implement io.ByteReader to avoid unnecessary read-ahead where applicable.

Return Value

io.Reader

Decompression reader; malformed input errors occur during reading.

Working Example

Example Snippetgo
package main
import (  "bytes"  "compress/bzip2"  "encoding/hex"  "fmt"  "io")
func must(e error) {  if e != nil {    panic(e)  }}func fixture() []byte {  p, e := hex.DecodeString("425a68393141592653591d2bf29e000000050000800000a000210082b1772453850901d2bf29e0")  must(e)  return p}func main() {  r := bzip2.NewReader(bytes.NewReader(fixture()))  p, e := io.ReadAll(r)  must(e)  fmt.Println(string(p))}
Go Reference