Standard library / archive/tar

archive/tar.NewReader()

Construct a sequential TAR reader. Call Next before reading each entry; names are untrusted metadata. Expected stdout: 'a.txt\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
rio.ReaderInput stream or random-access archive reader.

Return Value

*Reader

Reader for archive input.

Working Example

Example Snippetgo
package main
import (  "archive/tar"  "bytes"  "fmt")
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 main() {  r := tar.NewReader(bytes.NewReader(archive()))  h, e := r.Next()  must(e)  fmt.Println(h.Name)}
Go Reference