Standard library / archive/tar
archive/tar.FileInfoHeader()
Convert filesystem metadata into a TAR header. Supply link target separately; set Name explicitly for nested paths. Expected stdout: 'a.txt 2\n'.
Formal Syntax & Structure
go syntaxgo
func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fi | fs.FileInfo | Metadata for the source filesystem entry. |
| link | string | Link target for a symbolic link; otherwise empty. |
Return Value
(*Header, error)
Header and error, including EOF at end when reading.
Working Example
Example Snippetgo
package main
import ( "archive/tar" "fmt" "io/fs" "testing/fstest")
func must(e error) { if e != nil { panic(e) }}
func main() { fsys := fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("Go"), Mode: 0600}} fi, e := fs.Stat(fsys, "a.txt") must(e) h, e := tar.FileInfoHeader(fi, "") must(e) fmt.Println(h.Name, h.Size)}