example_test.go 767 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !plan9
  5. package fsnotify_test
  6. import (
  7. "log"
  8. "github.com/fsnotify/fsnotify"
  9. )
  10. func ExampleNewWatcher() {
  11. watcher, err := fsnotify.NewWatcher()
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. defer watcher.Close()
  16. done := make(chan bool)
  17. go func() {
  18. for {
  19. select {
  20. case event := <-watcher.Events:
  21. log.Println("event:", event)
  22. if event.Op&fsnotify.Write == fsnotify.Write {
  23. log.Println("modified file:", event.Name)
  24. }
  25. case err := <-watcher.Errors:
  26. log.Println("error:", err)
  27. }
  28. }
  29. }()
  30. err = watcher.Add("/tmp/foo")
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. <-done
  35. }