2026-05-06 00:18:09 +08:00
|
|
|
|
# 关于本项目
|
2026-04-23 21:56:30 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
本项目完全由 AI 维护。代码源自 github.com/ssgo/u 的重构。
|
2026-04-24 15:53:46 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
# @go/file
|
2026-04-24 15:53:46 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
`@go/file` 是一个为“高性能、内存虚拟化、消除摩擦”设计的 IO 工具库。它支持原生文件系统操作、内存文件系统(支持 Gzip 压缩存储)、以及智能对象序列化(JSON/YAML)。
|
2026-04-24 15:53:46 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
## 🎯 设计哲学
|
2026-05-01 21:04:09 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
* **内存文件系统 (Memory FS)**:支持将文件加载到内存中进行读写,极大提升了测试场景与高频小文件读写的性能。支持安全加载(基于 `go/safe`)与自动压缩存储。
|
|
|
|
|
|
* **消除摩擦 (Frictionless)**:废除 `Must` 前缀函数,全面结合 `go/cast` 的 `As` 函数。
|
|
|
|
|
|
* **智能序列化**:提供 `UnmarshalFile` 与 `MarshalFile`,自动处理路径创建与序列化格式。
|
2026-05-01 21:04:09 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
## 🛠 API Reference
|
2026-05-01 21:04:09 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
### 基础文件操作 (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) error`
|
|
|
|
|
|
- `func Write(filename string, content string) error`
|
|
|
|
|
|
- `func 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) error`
|
|
|
|
|
|
- `func Extract(srcFile, destDir string, stripRoot bool) error`
|
|
|
|
|
|
|
|
|
|
|
|
### 内存文件系统 (Memory FS)
|
|
|
|
|
|
- `func AddFileToMemory(mf MemFile)`
|
|
|
|
|
|
- `func ReadFileFromMemory(name string) *MemFile`
|
|
|
|
|
|
- `func SafeLoadFileToMemory(filename string)`
|
|
|
|
|
|
- `func LoadFilesToMemoryFromB64(b64File *MemFileB64)`
|
|
|
|
|
|
|
|
|
|
|
|
## 📦 安装
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
go get apigo.cc/go/file
|
2026-05-01 21:04:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
## 💡 示例
|
2026-05-01 21:04:09 +08:00
|
|
|
|
|
|
|
|
|
|
```go
|
2026-05-06 00:18:09 +08:00
|
|
|
|
import (
|
|
|
|
|
|
"apigo.cc/go/file"
|
|
|
|
|
|
"apigo.cc/go/cast"
|
|
|
|
|
|
)
|
2026-05-01 21:04:09 +08:00
|
|
|
|
|
2026-05-06 00:18:09 +08:00
|
|
|
|
// 读取文件内容,消除错误摩擦
|
|
|
|
|
|
content := cast.As(file.Read("config.txt"))
|
2026-05-01 21:04:09 +08:00
|
|
|
|
```
|