Last active
July 13, 2020 12:16
-
-
Save surajnarwade/58926d40618e14cc9203dd8fe9aa0b9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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