filelock

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2018 License: MIT Imports: 5 Imported by: 1

README

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

Documentation

Overview

Example (FileLockNewAndUnlock)
lockHandle, err := filelock.New("myLockFile.lock")

if err != nil && err == filelock.ErrFileIsBeingUsed {
	return
}

if err != nil {
	fmt.Println(err)
	return
}

// do main program
fmt.Println("running")

err = lockHandle.Unlock()

if err != nil {
	fmt.Println(err)
}
Output:

running

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrFileIsBeingUsed is returned when file is in use by another program
	ErrFileIsBeingUsed = errors.New("file is being used by another process")
	// ErrFileAlreadyUnlocked is returned when file has already been unlocked or was not locked
	ErrFileAlreadyUnlocked = errors.New("file already unlocked")
)

Functions

This section is empty.

Types

type LockHandle

type LockHandle struct {
	// contains filtered or unexported fields
}

LockHandle to be used for locking and unlocking the file

func New

func New(filename string) (*LockHandle, error)

New creates a new file lock

func (*LockHandle) Lock

func (lockHandle *LockHandle) Lock() error

Lock locks the file lock

func (*LockHandle) Unlock

func (lockHandle *LockHandle) Unlock() error

Unlock unlocks the file lock

Jump to

Keyboard shortcuts

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