Golang file lock to run only one instance of an application at a time
go get github.com/MichaelS11/go-file-lock
import (
"github.com/MichaelS11/go-file-lock"
)
func Run() error {
lockHandle, err := filelock.New("myLockFile.lock")
if err != nil && err == filelock.ErrFileIsBeingUsed {
return nil
}
if err != nil {
return err
}
# do main program
return lockHandle.Unlock()
}
If you want the current directory of your application, you can do:
baseDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))