Skip to content

Conversation

ldez
Copy link
Contributor

@ldez ldez commented Jun 27, 2024

The interface sync.Locker is:

type Locker interface {
	Lock()
	Unlock()
}

https://github.com/golang/go/blob/5a18e79687dea15680ff5f799b549fa0efd0cad9/src/sync/mutex.go#L41-L45

But the method signatures of Flock are:

type Foo interface {
	Lock() error
	Unlock() error
}

So to implement sync.Locker interface, a wrapper should be used.

Example:

type Wrapper struct {
	f *Flock
}

func NewWrapper(path string) *Wrapper {
	return &Wrapper{f: flock.New(path)}
}

func (w Wrapper) Lock() {
	err := w.f.Lock()
	if err != nil {
		panic(err)
	}
}

func (w Wrapper) Unlock() {
	err := w.f.Unlock()
	if err != nil {
		panic(err)
	}
}

Fixes #45

@ldez ldez merged commit 0533797 into gofrs:master Jun 27, 2024
@ldez ldez deleted the fix/readme-sync-locker branch June 27, 2024 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

docs: does not implement sync.Locker
1 participant