files — 文件
import "gomaton.dev/sdk/files"
Package files 设备侧文件系统(纯 Go 标准库)。路径为设备绝对路径;项目 resources 下用 Path() 取。
能力总览:
- 判断:Exists / IsFile / IsDir。
- 读写:Read / ReadBytes、Write / WriteBytes、Append 追加(均自动建父目录)。
- 目录:EnsureDir、ListDir 列一级、ClearDir 清空。
- 增删移:Copy / Move / Rename / Remove。
- 路径与摘要:Path 取 resources 相对路径、GetName / GetNameWithoutExtension / GetExtension、GetMd5。
Append
func Append(path, text string) error
Append 追加文本(不存在则创建)。
ClearDir
func ClearDir(path string) (int, error)
ClearDir 清空目录内容,返回删掉的条目数。
Copy
func Copy(from, to string) error
Copy 复制文件内容 from → to(覆盖;to 的父目录自动创建)。
EnsureDir
func EnsureDir(path string) error
EnsureDir 确保目录存在(递归)。
Exists
func Exists(path string) bool
Exists 路径是否存在(文件或目录)。
GetExtension
func GetExtension(path string) string
GetExtension 取扩展名(不含点,如 "png");无扩展名返回空串。
GetMd5
func GetMd5(path string) (string, error)
GetMd5 文件 MD5(hex)。
GetName
func GetName(path string) string
GetName 取路径的文件名部分(含扩展名)。
GetNameWithoutExtension
func GetNameWithoutExtension(path string) string
GetNameWithoutExtension 取文件名并去掉扩展名。
IsDir
func IsDir(path string) bool
IsDir 路径存在且是目录。
IsFile
func IsFile(path string) bool
IsFile 路径存在且是普通文件。
ListDir
func ListDir(path string) ([]Entry, error)
ListDir 列目录(仅一级)。
Move
func Move(from, to string) error
Move 移动 from → to(to 的父目录自动创建)。
Path
func Path(rel string) string
Path 把 resources 下的相对路径变成设备绝对路径,如 Path("tpl/ok.png")。
Read
func Read(path string) (string, error)
Read 读取整个文件为字符串。
ReadBytes
func ReadBytes(path string) ([]byte, error)
ReadBytes 读取整个文件为字节。
Remove
func Remove(path string) error
Remove 删除文件或目录(递归)。
Rename
func Rename(path, newName string) error
Rename 在原目录内把 path 改名为 newName(仅文件名,不含目录)。
ResDir
func ResDir() string
ResDir 项目 resources 目录在设备上的位置:取环境变量 AGOV_RES(agov run 会设), 没有则取可执行文件旁的 resources/。
Write
func Write(path, text string) error
Write 写入文本(覆盖;父目录不存在则自动创建)。
WriteBytes
func WriteBytes(path string, data []byte) error
WriteBytes 写入字节(覆盖;父目录不存在则自动创建)。