This commit is contained in:
xinyu 2026-02-21 17:48:05 +08:00
parent 117c2be9de
commit 0e8ba8633a
1 changed files with 11 additions and 0 deletions

11
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"net/http"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
@ -42,4 +43,14 @@ func main() {
if _, err := bfs.Open("/etc/passwd"); err != nil { if _, err := bfs.Open("/etc/passwd"); err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
//当作http.FileSystem
memFs := afero.NewMemMapFs()
afero.WriteFile(memFs, "index.html", []byte("<h1>Afero in web</h1>"), 0644)
httpFs := afero.NewHttpFs(memFs)
http.Handle("/", http.FileServer(httpFs))
//显示index.html文件内容
http.ListenAndServe(":8080", nil)
} }