This commit is contained in:
xinyu 2026-02-21 14:45:22 +08:00
parent ad62d9b3e5
commit 117c2be9de
2 changed files with 26 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
afero-demo afero-demo
*.exe *.exe
go.sum go.sum
*.txt

24
main.go
View File

@ -18,4 +18,28 @@ func main() {
} }
fmt.Println(string(data)) fmt.Println(string(data))
//真实文件系统
fs = afero.NewOsFs()
afero.WriteFile(fs, "poem.txt", []byte("月光光,照大床(文件系统)"), 0644)
data, err = afero.ReadFile(fs, "poem.txt")
if err != nil {
panic(err)
}
fmt.Println(string(data))
//拷贝文件
err = afero.WriteFile(fs, "poem_copy.txt", data, 0644)
if err != nil {
panic(err)
}
//限定根目录
bfs := afero.NewBasePathFs(afero.NewOsFs(), "/home/yy/project")
if _, err := bfs.Open("/etc/passwd"); err != nil {
fmt.Println(err.Error())
}
} }