func main() { if len(os.Args) == 1 { log.Fatal("Argument required: <lock file>") } lockFilePath := os.Args[1] lockFile, err := os.OpenFile(lockFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600) if err != nil { log.Fatal(err) } defer lockFile.Close() err = lockapi.LockFile(lockFile) if err != nil { log.Fatal(err) } fmt.Println("Press ENTER to unlock...") fmt.Scanln() err = lockapi.UnlockFile(lockFile) if err != nil { log.Fatal(err) } fmt.Println("Press ENTER to end...") fmt.Scanln() }
func TestBlockingFileLockingCalls(t *testing.T) { lockFile, err := ioutil.TempFile(os.TempDir(), "LockAPI_TestFile") if err != nil { t.Fatal(err) } defer func() { lockFile.Close() os.Remove(lockFile.Name()) }() err = lockapi.LockFile(lockFile) if err != nil { t.Fatal(err) } err = lockapi.UnlockFile(lockFile) if err != nil { t.Fatal(err) } }