Documentation
¶
Overview ¶
Package remSys 提供系统和文件操作的工具函数。 跨平台支持,可在 Windows、Linux、macOS 上运行。
Index ¶
- func AbsPath(path string) (string, error)
- func AppendFile(path string, content string) error
- func BaseName(path string) string
- func DirExists(path string) bool
- func DirName(path string) string
- func EnsureDir(path string) error
- func EnsureParentDir(filePath string) error
- func Exists(path string) bool
- func Ext(path string) string
- func FileExists(path string) bool
- func GetFileSize(path string) (int64, error)
- func GetFileSizeOrZero(path string) int64
- func GracefulShutdown() <-chan struct{}
- func IsDir(path string) bool
- func IsFile(path string) bool
- func JoinPath(elem ...string) string
- func OnShutdown(callback func())
- func OnShutdownAsync(callback func())
- func ReadFile(path string) (string, error)
- func WaitForShutdown() os.Signal
- func WaitForSignal(signals ...os.Signal) os.Signal
- func WaitForSignalContext(signals ...os.Signal) context.Context
- func WriteFile(path string, content string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendFile ¶
AppendFile 向文件追加内容。 如果文件不存在会创建。
func EnsureDir ¶
EnsureDir 确保目录存在,不存在则创建(包括所有父目录)。
示例:
EnsureDir("/path/to/dir") // 创建 /path/to/dir 及其父目录
func EnsureParentDir ¶
EnsureParentDir 确保文件的父目录存在。
示例:
EnsureParentDir("/path/to/file.txt") // 确保 /path/to 存在
func GetFileSize ¶
GetFileSize 获取文件大小(字节)。 如果文件不存在或发生错误,返回 -1 和错误。
func GetFileSizeOrZero ¶
GetFileSizeOrZero 获取文件大小,出错时返回 0。
func GracefulShutdown ¶
func GracefulShutdown() <-chan struct{}
GracefulShutdown 提供优雅停机的便捷方法。 返回一个 channel,在收到关闭信号时会被关闭。
示例:
done := GracefulShutdown() <-done // 阻塞直到收到关闭信号 cleanup()
func OnShutdown ¶
func OnShutdown(callback func())
OnShutdown 注册一个关闭回调函数。 当收到 SIGINT 或 SIGTERM 信号时,回调函数会被调用。 该函数会阻塞直到收到信号。
示例:
OnShutdown(func() {
log.Println("正在关闭服务...")
db.Close()
server.Shutdown()
})
func OnShutdownAsync ¶
func OnShutdownAsync(callback func())
OnShutdownAsync 在后台 goroutine 中注册一个关闭回调。 非阻塞版本。
示例:
OnShutdownAsync(func() {
cleanup()
})
// 继续其他工作...
func WaitForShutdown ¶
WaitForShutdown 等待关闭信号(SIGINT 或 SIGTERM)。 这是 WaitForSignal 的便捷版本。
示例:
go func() {
sig := WaitForShutdown()
log.Printf("收到关闭信号: %v", sig)
}()
func WaitForSignal ¶
WaitForSignal 等待并返回接收到的信号。 如果未指定信号,默认等待 SIGINT 和 SIGTERM。
示例:
sig := WaitForSignal() // 等待 Ctrl+C 或 kill 信号
fmt.Println("收到信号:", sig)
func WaitForSignalContext ¶
WaitForSignalContext 返回一个在收到信号时自动取消的 context。 如果未指定信号,默认监听 SIGINT 和 SIGTERM。
示例:
ctx := WaitForSignalContext() server.Serve(ctx)
Types ¶
This section is empty.