Standard library / archive/tar

archive/tar.Writer.Write()

Write entry payload after WriteHeader. It is an error to exceed declared Size. Expected stdout: '2\n'.

Formal Syntax & Structure

go syntaxgo
func (tw *Writer) Write(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")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  w := tar.NewWriter(&b)  must(w.WriteHeader(&tar.Header{Name: "a", Size: 2}))  n, e := w.Write([]byte("Go"))  must(e)  must(w.Close())  fmt.Println(n)}
Go Reference