I constantly write to one file some data everytime requests come in. I also want to parse this file and read from it sometimes. How can I do this reading if the file is cons
You could make use of sync.RWMutex. Then:
RLock()
, read, then call RUnlock()
.Lock()
, write, then call Unlock()
.As long as you do that, you're ensuring that:
Here's a very little example:
package sample
import (
"sync"
)
var fileMutex = new(sync.RWMutex)
func readFile() {
fileMutex.RLock()
defer fileMutex.RUnlock()
// Read the file. Don't modify it.
}
func writeFile() {
fileMutex.Lock()
defer fileMutex.Unlock()
// Write to the file.
}