go-file-lock: github.com/MichaelS11/go-file-lock Index | Examples | Files

package filelock

import "github.com/MichaelS11/go-file-lock"

Code:

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

Package Files

fileLock.go withFlock.go

Variables

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

type LockHandle Uses

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

LockHandle to be used for locking and unlocking the file

func New Uses

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

New creates a new file lock

func (*LockHandle) Lock Uses

func (lockHandle *LockHandle) Lock() error

Lock locks the file lock

func (*LockHandle) Unlock Uses

func (lockHandle *LockHandle) Unlock() error

Unlock unlocks the file lock

Package filelock imports 5 packages (graph). Updated 2018-10-12. Refresh now. Tools for package owners.