Skip to content

Instantly share code, notes, and snippets.

@surajnarwade
Last active July 13, 2020 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surajnarwade/58926d40618e14cc9203dd8fe9aa0b9a to your computer and use it in GitHub Desktop.
Save surajnarwade/58926d40618e14cc9203dd8fe9aa0b9a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"gopkg.in/fsnotify.v1"
)
// main
func main() {
// creates a new file watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("ERROR", err)
}
defer watcher.Close()
done := make(chan bool)
suraj := make(chan bool)
//
go func() {
for {
select {
// watch for events
case event := <-watcher.Events:
fmt.Printf("EVENT! %#v, WRITE: %#v, RENAME: %#v\n", event, event.Op&fsnotify.Write == fsnotify.Write, event.Op&fsnotify.Rename == fsnotify.Rename)
if event.Op&fsnotify.Write == fsnotify.Write {
suraj <- true
}
// watch for errors
case err := <-watcher.Errors:
fmt.Println("ERROR", err)
}
}
}()
go func() {
for {
if <-suraj {
fmt.Println("FILE CHANGED")
}
}
}()
// out of the box fsnotify can watch a single file, or a single directory
if err := watcher.Add("/home/suraj/fsnotify-1/certs"); err != nil {
fmt.Println("ERROR", err)
}
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment