Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Latest commit

 

History

History
44 lines (29 loc) · 1.1 KB

README.md

File metadata and controls

44 lines (29 loc) · 1.1 KB

Go file lock

GoDoc Reference Build Status Coverage Go Report Card

Golang file lock to run only one instance of an application at a time

Get

go get github.com/MichaelS11/go-file-lock

Usage

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]))