check file existence without using os.Stat after locking the path

608 Views Asked by At

When I lock a file path with flock then check the file existence it return no error even though there is no file in that path. The code follows:

filePath := filepath.Join(r.path, fmt.Sprintf("%s_event.json", eventId))

fileLock := flock.New(filePath)
fileLock.Lock()
defer fileLock.Close()

_, err = os.Stat(filePath)
if err != nil {
    if os.IsNotExist(errs) {
        return event, EventNotFound{}
    }   
    return
}     

But when at first check the Stat then lock the file it works. I need to check it before that. Every idea is welcome!

0

There are 0 best solutions below