Standard library / os
os.CopyFS()
Copies a source fs.FS into a destination directory, creating it as needed; existing files cause an error and partial changes can remain. Existing destination symlinks can be followed. Example uses a disposable temporary workspace. Expected stdout: 'abc\n'.
Formal Syntax & Structure
go syntaxgo
func CopyFS(dir string, fsys fs.FS) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| dir | string | Directory pathname. |
| fsys | fs.FS | Source filesystem. |
Return Value
error
Copies a source fs.FS into a destination directory, creating it as needed; existing files cause an error and partial changes can remain.
Working Example
Example Snippetgo
package main
import ( "fmt" "os" "testing/fstest")
func check(e error) { if e != nil { panic(e) }}func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()() check(os.CopyFS("out", fstest.MapFS{"a.txt": {Data: []byte("abc")}})) b, e := os.ReadFile("out/a.txt") check(e) fmt.Println(string(b))}