xfile

package
v1.4.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	DefaultFlushSizeLimit = 1 << 20
	DefaultFlushInterval  = 1 * time.Second
	MinFlushSizeLimit     = 4096
	MinFlushInterval      = 100 * time.Millisecond
)

Variables

View Source
var ErrFilename = errors.New("wrong file name")

Functions

func CopyDir

func CopyDir(srcDir, dstDir string) error

CopyDir 目录拷贝

func CopyFile

func CopyFile(srcFile, dstFile string) error

CopyFile 文件拷贝

func HeadLines

func HeadLines(filename string, num int) ([]string, error)

HeadLines 读取文件头部 N 行

func IsDir

func IsDir(s string) bool

IsDir 目录是否存在

func IsExist

func IsExist(s string) bool

IsExist 文件或目录是否存在

func IsFile

func IsFile(s string) bool

IsFile 文件是否存在

func ModTime

func ModTime(filename string) time.Time

ModTime 文件最后修改时间

func ReadFile

func ReadFile(filename string) (string, error)

ReadFile reads contents from a file

func ReadLines

func ReadLines(filename string) ([]string, error)

ReadLines reads contents from a file and splits them by new lines. A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).

func ReadLinesOffsetN

func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error)

ReadLinesOffsetN reads contents from file and splits them by new line. The offset tells at which line number to start. The count determines the number of lines to read (starting from offset): n >= 0: at most n lines n < 0: whole file Ref: gopsutil

无末尾换行的最后一行与普通行一样受 offset/n 约束 (offset 超过总行数时返回空). 只有 EOF 视为正常结束; 其他读取错误 (如目标是目录) 连同已读到的行一起返回, 不伪装成成功.

func ResetDir

func ResetDir(dirPath string) error

ResetDir 清除并重建空目录

func TailLines

func TailLines(filename string, num int, cleanLine ...bool) ([]string, error)

TailLines 返回文件最后 N 行

func UnzipDir

func UnzipDir(zipFile, dstDir string) error

UnzipDir 解压 zip 到目录. 目标目录不存在时自动创建. 所有写入经 os.Root 约束在 dstDir 内: 条目名含 ".." 直接拒绝, 目标树中已有的符号链接可以指向树内, 但指向树外 (或为绝对路径链接) 时返回错误, 不会写出目标目录. 文件条目的父目录按需创建, 合法 ZIP 缺少显式目录条目或文件条目先于目录条目时同样可以解压. 遇到错误立即返回, 此前已解压的条目保留.

func UnzipFile

func UnzipFile(zipFile *zip.File, dstFile string) error

UnzipFile 解压单个文件

func ZipDir

func ZipDir(srcDir, zipFilePath string) (err error)

ZipDir 将目录打包为 zip 文件, 条目统一使用正斜杠. 遍历、ZIP 收尾和目标文件关闭错误均返回, 失败时可能保留未完成的输出文件.

Types

type DefaultFilename

type DefaultFilename struct{}

func (*DefaultFilename) MakeFilename

func (d *DefaultFilename) MakeFilename(name string) string

type FilenameMaker

type FilenameMaker interface {
	MakeFilename(name string) string
}

type Logger

type Logger interface {
	Errorf(format string, v ...any)
}

type Options

type Options struct {
	// 文件名生成器, 每秒检查
	FilenameMaker FilenameMaker

	// 日志处理器
	Logger Logger

	// 每次(实例启动时除外)滚动的文件是否删除后重建
	Rebuild bool

	// 刷新到磁盘的大小和时间间隔, 默认 1MiB, 1秒
	// 注意: 由于写文件有缓冲, 如果按秒切割文件, 数据将有可能写入上一秒的文件名中
	FlushSizeLimit int
	FlushInterval  time.Duration
}

type Roller

type Roller struct {
	// contains filtered or unexported fields
}

func NewRoller

func NewRoller(filename string, opt *Options) (*Roller, error)

NewRoller 创建按文件名滚动的写入器. opt 为 nil 时使用默认 maker / logger / 刷新阈值, 避免零间隔 ticker panic. 必须先归一化选项再打开文件并启动刷新协程, 否则首次缓冲大小和 Rebuild 语义都不生效.

func (*Roller) Close

func (r *Roller) Close()

Close 刷新剩余缓冲并关闭当前文件, 同时通知刷新协程退出. 调用方必须保证只关闭一次; 重复关闭会再次 close 内部通道.

func (*Roller) Write

func (r *Roller) Write(p []byte) (int, error)

func (*Roller) WriteString

func (r *Roller) WriteString(s string) (int, error)

type TimeBasedFilename

type TimeBasedFilename struct {
	// 文件目录
	FilePath string

	// 文件名模板: run-%s.log
	FilenameTpl string

	// 日期时间模板: 060102
	TimeTpl string

	// 时间标识相同则不生成新文件名: 221130
	TimeTag string
}

func (*TimeBasedFilename) MakeFilename

func (t *TimeBasedFilename) MakeFilename(name string) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL