Standard library / archive/tar

archive/tar.Reader.Read()

Read current entry payload; EOF marks its boundary, not necessarily the end of the archive. Expected stdout: 'Go true\n'.

Formal Syntax & Structure

go syntaxgo
func (tr *Reader) Read(b []byte) (int, error)

Parameters & Attributes

NameTypeDescription
b[]byteByte buffer for reading entry contents or writing payload bytes.

Return Value

(int, error)

Bytes processed and error; check both.

Working Example

Example Snippetgo
package main
import (  "archive/tar"  "bytes"  "fmt"  "io")
func must(e error) {  if e != nil {    panic(e)  }}func archive() []byte {  var b bytes.Buffer  w := tar.NewWriter(&b)  must(w.WriteHeader(&tar.Header{Name: "a.txt", Mode: 0600, Size: 2}))  _, e := w.Write([]byte("Go"))  must(e)  must(w.Close())  return b.Bytes()}func reader() *tar.Reader { return tar.NewReader(bytes.NewReader(archive())) }func main() {  r := reader()  _, e := r.Next()  must(e)  p := make([]byte, 3)  n, e := r.Read(p)  fmt.Println(string(p[:n]), e == io.EOF)}
Go Reference