From 117c2be9de81856ab94bea79b50c5409786c63c0 Mon Sep 17 00:00:00 2001 From: xinyu Date: Sat, 21 Feb 2026 14:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- main.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index cb30448..c183848 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ afero-demo *.exe -go.sum \ No newline at end of file +go.sum +*.txt \ No newline at end of file diff --git a/main.go b/main.go index daec585..293eec8 100644 --- a/main.go +++ b/main.go @@ -18,4 +18,28 @@ func main() { } 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()) + } }