github.com/gofrs/flock

Thread-safe file locking library in Go (originally github.com/theckman/go-flock)


Keywords
file, flock, go, golang, golang-library, linux, locking, osx, windows
License
BSD-3-Clause
Install
go get github.com/gofrs/flock

Documentation

flock

TravisCI Build Status GoDoc License Go Report Card

flock implements a thread-safe sync.Locker interface for file locking. It also includes a non-blocking TryLock() function to allow locking without blocking execution.

License

flock is released under the BSD 3-Clause License. See the LICENSE file for more details.

Go Compatibility

This package makes use of the context package that was introduced in Go 1.7. As such, this package has an implicit dependency on Go 1.7+.

Installation

go get -u github.com/gofrs/flock

Usage

import "github.com/gofrs/flock"

fileLock := flock.New("/var/lock/go-lock.lock")

locked, err := fileLock.TryLock()

if err != nil {
	// handle locking error
}

if locked {
	// do work
	fileLock.Unlock()
}

For more detailed usage information take a look at the package API docs on GoDoc.