1.8 KiB
1.8 KiB
关于本项目
本项目完全由 AI 维护。代码源自 github.com/ssgo/u 的重构。
@go/file
@go/file 是一个为“高性能、内存虚拟化、消除摩擦”设计的 IO 工具库。它支持原生文件系统操作、内存文件系统(支持 Gzip 压缩存储)、以及智能对象序列化(JSON/YAML)。
🎯 设计哲学
- 内存文件系统 (Memory FS):支持将文件加载到内存中进行读写,极大提升了测试场景与高频小文件读写的性能。支持安全加载(基于
go/safe)与自动压缩存储。 - 消除摩擦 (Frictionless):废除
Must前缀函数,全面结合go/cast的As函数。 - 智能序列化:提供
UnmarshalFile与MarshalFile,自动处理路径创建与序列化格式。
🛠 API Reference
基础文件操作 (Frictionless with cast.As)
func ReadBytes(filename string) ([]byte, error)func Read(filename string) (string, error)func ReadLines(filename string) ([]string, error)func WriteBytes(filename string, content []byte) errorfunc Write(filename string, content string) errorfunc Exists(filename string) bool
压缩与归档
func Compress(data []byte, cType string) ([]byte, error)func Decompress(data []byte, cType string) ([]byte, error)func Archive(srcPath, destFile string) errorfunc Extract(srcFile, destDir string, stripRoot bool) error
内存文件系统 (Memory FS)
func AddFileToMemory(mf MemFile)func ReadFileFromMemory(name string) *MemFilefunc SafeLoadFileToMemory(filename string)func LoadFilesToMemoryFromB64(b64File *MemFileB64)
📦 安装
go get apigo.cc/go/file
💡 示例
import (
"apigo.cc/go/file"
"apigo.cc/go/cast"
)
// 读取文件内容,消除错误摩擦
content := cast.As(file.Read("config.txt"))