Standard library / archive/tar

archive/tar.Writer.AddFS()

Walk a filesystem and add its files and directories. Go 1.23 introduced AddFS; arbitrary special file types are not supported. Expected stdout: 'a.txt 2\n'.

Formal Syntax & Structure

go syntaxgo
func (tw *Writer) AddFS(fsys fs.FS) error

Parameters & Attributes

NameTypeDescription
fsysfs.FSFilesystem to traverse and add; use a trusted source.

Return Value

error

Nil on success, otherwise operation error.

Working Example

Example Snippetgo
package main
import (  "archive/tar"  "bytes"  "fmt"  "testing/fstest")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  w := tar.NewWriter(&b)  fsys := fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("Go"), Mode: 0600}}  must(w.AddFS(fsys))  must(w.Close())  r := tar.NewReader(&b)  h, e := r.Next()  must(e)  fmt.Println(h.Name, h.Size)}
Go Reference