singletonUtil

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package singletonUtil 提供单实例运行检测功能 使用文件锁机制确保同一时间只有一个程序实例在运行

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSingleton

func IsSingleton() bool

IsSingleton 检查当前程序是否只有一个实例在运行,使用文件锁机制 返回值:

true - 当前程序是唯一运行的实例
false - 检测到其他相同程序实例正在运行,或无法获取文件锁

机制说明:

  • 在系统临时目录创建基于程序路径的锁文件
  • 使用 github.com/gofrs/flock 库实现跨平台文件锁
  • 当程序正常退出时,锁会自动释放

使用示例:

// 在 main 函数开头调用
defer SingletonUtil.ReleaseInstanceLock()
if !SingletonUtil.IsSingleton() {
    log.Fatal("程序已在运行")
}

func ReleaseInstanceLock

func ReleaseInstanceLock()

ReleaseInstanceLock 释放实例锁(在程序退出时调用) 通常在 main 函数开头使用 defer 调用:

defer SingletonUtil.ReleaseInstanceLock()

Types

This section is empty.

Jump to

Keyboard shortcuts

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