Last active
October 1, 2021 04:08
-
-
Save sdomino/746006ac626d6d00598a to your computer and use it in GitHub Desktop.
How to detect file changes in Golang: https://medium.com/@skdomino/watch-this-file-watching-in-go-5b5a247cf71f
This file contains 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" | |
"github.com/go-fsnotify/fsnotify" | |
) | |
// 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) | |
// | |
go func() { | |
for { | |
select { | |
// watch for events | |
case event := <-watcher.Events: | |
fmt.Printf("EVENT! %#v\n", event) | |
// watch for errors | |
case err := <-watcher.Errors: | |
fmt.Println("ERROR", err) | |
} | |
} | |
}() | |
// out of the box fsnotify can watch a single file, or a single directory | |
if err := watcher.Add("/Users/skdomino/Desktop/test.html"); err != nil { | |
fmt.Println("ERROR", err) | |
} | |
<-done | |
} |
Thank you for the code.
Please update github.com/go-fsnotify/fsnotify
to github.com/fsnotify/fsnotify
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
❤️