spf13/afero 通用文件系统试用

以前有大概介绍过类似的几个不错的通用文件系统工具包,以下是关于spf13/afero 的试用

参考代码

package main
import (
    "io/ioutil"
    "log"
    "github.com/spf13/afero"
)
func main() {
    var appFs = afero.NewMemMapFs()
    log.Println(appFs.Name())
    appFs.Mkdir("/", 0755)
    fh, _ := appFs.Create("/demoapp.js")
    fh.WriteString("This is a test")
    fh.Close()
    file, err := appFs.Open("/demoapp.js")
    defer file.Close()
    if err != nil {
        log.Println("open file error:", err.Error())
    }
    bytes, err := ioutil.ReadAll(file)
    if err != nil {
        log.Println("read file error:", err.Error())
    }
    log.Println(string(bytes))
}

集成http server 方法

package main
import (
    "io/ioutil"
    "log"
    "net/http"
    "github.com/spf13/afero"
)
func httpFs() {
    var appFs = afero.NewMemMapFs()
    appFs.Mkdir("/", 0755)
    fh, _ := appFs.Create("/demoapp.js")
    fh.WriteString("This is a test")
    fh.Close()
    httpFs := afero.NewHttpFs(appFs)
    fileserver := http.FileServer(httpFs.Dir("/"))
    http.Handle("/", fileserver)
    http.ListenAndServe(":8090", nil)
}
func main() {
    httpFs()
}

说明

目前官方也在计划实现一个通用的文件系统,抽象层,比较期待ga,基于通用文件系统的好处是,方便测试,同时屏蔽了
对于多种异构文件系统的访问

参考资料

https://github.com/aos-dev/go-storage
https://github.com/lytics/cloudstorage
https://github.com/graymeta/stow
https://github.com/rclone/rclone
https://gocloud.dev/howto/blob/
https://github.com/spf13/afero
https://github.com/google/go-cloud/tree/master/blob
https://github.com/gregjones/httpcache

posted on   荣锋亮  阅读(903)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-01-05 0x nodejs火焰图工具试用
2019-01-05 gitingore && opensource license 自动生成的网站
2017-01-05 spring retry 使用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示