package
Version:
v0.3.6
Opens a new window with list of versions in this module.
Published: Jan 15, 2026
License: MIT
Opens a new window with license information.
Imports: 5
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
File (文件)
提供简单的文件读写辅助功能。
API
ReadRemoteFile
- 签名:
func ReadRemoteFile(url string) ([]byte, error)
- 描述: 读取远程 URL 的文件内容。
- 注意: 全量加载到内存,不适合大文件。
WriteLocalFile
- 签名:
func WriteLocalFile(path string, bytes []byte) error
- 描述: 将字节内容写入本地文件。
- 特性: 自动创建缺失的目录,覆盖已存在的文件。
示例
import (
"fmt"
"github.com/fireflycore/go-utils/file"
)
func main() {
// 读取远程
data, err := file.ReadRemoteFile("https://example.com/data.json")
if err != nil {
panic(err)
}
// 写入本地
err = file.WriteLocalFile("./data/backup.json", data)
if err != nil {
panic(err)
}
}
Documentation
¶
ReadRemoteFile 读取远程文件内容
url: 远程文件的 URL 地址
返回: 文件内容的字节切片。如果下载失败或读取失败,返回 error
注意:此函数会将整个文件加载到内存,不适合读取大文件
WriteLocalFile 将内容写入本地文件
path: 本地文件路径(包含文件名)
bytes: 要写入的字节内容
返回: 错误信息
注意: 如果目录不存在,会自动创建;如果文件已存在,会覆盖
Source Files
¶
Click to show internal directories.
Click to hide internal directories.