I need to implement the following behavior in GO:
- A process should be able to read a file irrespective if any other process has locked the file for writing
- A process should obtain a write lock, before it can write to the file. This is to ensure that multiple processes cannot write to the same file
- A process should not wait to obtain the write lock, if it cannot obtain a lock it should move on
For UNIX based systems, syscall package in GO defines flock function, which could be used to implement the above behaviour in the following manner:
- Use syscall.flock function with LOCK_EX | LOCK_NB to try and obtain a lock before writing to the file
- Do not check for any locks before reading from the file
syscall package for Windows in GO, does not include flock. Given this, how best can I write code that can execute cross-platform and has the behavior described above?
I want to try and achieve this without making OS specific calls or using unsafe.
PS: I do not want mandatory file locking, the processes will check for file lock before performing file operations