remSys

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package remSys 提供系统和文件操作的工具函数。 跨平台支持,可在 Windows、Linux、macOS 上运行。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsPath

func AbsPath(path string) (string, error)

AbsPath 获取绝对路径。

func AppendFile

func AppendFile(path string, content string) error

AppendFile 向文件追加内容。 如果文件不存在会创建。

func BaseName

func BaseName(path string) string

BaseName 获取路径的最后一个元素(文件名或目录名)。

func DirExists

func DirExists(path string) bool

DirExists 判断目录是否存在且为目录。

func DirName

func DirName(path string) string

DirName 获取路径的目录部分。

func EnsureDir

func EnsureDir(path string) error

EnsureDir 确保目录存在,不存在则创建(包括所有父目录)。

示例:

EnsureDir("/path/to/dir")  // 创建 /path/to/dir 及其父目录

func EnsureParentDir

func EnsureParentDir(filePath string) error

EnsureParentDir 确保文件的父目录存在。

示例:

EnsureParentDir("/path/to/file.txt")  // 确保 /path/to 存在

func Exists

func Exists(path string) bool

Exists 判断文件或目录是否存在。

func Ext

func Ext(path string) string

Ext 获取文件扩展名。

func FileExists

func FileExists(path string) bool

FileExists 判断文件是否存在且为普通文件。

func GetFileSize

func GetFileSize(path string) (int64, error)

GetFileSize 获取文件大小(字节)。 如果文件不存在或发生错误,返回 -1 和错误。

func GetFileSizeOrZero

func GetFileSizeOrZero(path string) int64

GetFileSizeOrZero 获取文件大小,出错时返回 0。

func GracefulShutdown

func GracefulShutdown() <-chan struct{}

GracefulShutdown 提供优雅停机的便捷方法。 返回一个 channel,在收到关闭信号时会被关闭。

示例:

done := GracefulShutdown()
<-done  // 阻塞直到收到关闭信号
cleanup()

func IsDir

func IsDir(path string) bool

IsDir 判断路径是否为目录。

func IsFile

func IsFile(path string) bool

IsFile 判断路径是否为文件。

func JoinPath

func JoinPath(elem ...string) string

JoinPath 连接路径。

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 ReadFile

func ReadFile(path string) (string, error)

ReadFile 读取文件内容为字符串。

func WaitForShutdown

func WaitForShutdown() os.Signal

WaitForShutdown 等待关闭信号(SIGINT 或 SIGTERM)。 这是 WaitForSignal 的便捷版本。

示例:

go func() {
    sig := WaitForShutdown()
    log.Printf("收到关闭信号: %v", sig)
}()

func WaitForSignal

func WaitForSignal(signals ...os.Signal) os.Signal

WaitForSignal 等待并返回接收到的信号。 如果未指定信号,默认等待 SIGINT 和 SIGTERM。

示例:

sig := WaitForSignal()  // 等待 Ctrl+C 或 kill 信号
fmt.Println("收到信号:", sig)

func WaitForSignalContext

func WaitForSignalContext(signals ...os.Signal) context.Context

WaitForSignalContext 返回一个在收到信号时自动取消的 context。 如果未指定信号,默认监听 SIGINT 和 SIGTERM。

示例:

ctx := WaitForSignalContext()
server.Serve(ctx)

func WriteFile

func WriteFile(path string, content string) error

WriteFile 将字符串写入文件。 如果文件不存在会创建,存在则覆盖。

Types

This section is empty.

Jump to

Keyboard shortcuts

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