fswatch is a simple fsnotify wrapper to watch file updates correctly.
go get github.com/sagernet/fswatch
package main
import (
"log"
"github.com/sagernet/fswatch"
)
func main() {
var watchPath []string
watchPath = append(watchPath, "/tmp/my_file")
watcher, err := fswatch.NewWatcher(fswatch.Options{
Path: watchPath,
Callback: func(path string) {
log.Println("file updated: ", path)
},
})
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Block main goroutine forever.
<-make(chan struct{})
}