watch-file.go 525 B

12345678910111213141516171819202122232425
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/fsnotify/fsnotify"
  5. )
  6. func main(){
  7. watcher, _ := fsnotify.NewWatcher()
  8. watcher.Add("test.txt")
  9. defer watcher.Close();
  10. select {
  11. case event := <-watcher.Events:
  12. {
  13. if event.Op&fsnotify.Rename == fsnotify.Rename || event.Op&fsnotify.Remove == fsnotify.Remove {
  14. fmt.Println("delete")
  15. } else if event.Op&fsnotify.Write == fsnotify.Write {
  16. } else if event.Op&fsnotify.Create == fsnotify.Create {
  17. }
  18. }
  19. case err := <-watcher.Errors:
  20. fmt.Println(err.Error())
  21. }
  22. }