Skip to content

Instantly share code, notes, and snippets.

@armhold
Created March 26, 2016 18:36
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 armhold/a7f4d6666dcbcbbe1faf to your computer and use it in GitHub Desktop.
Save armhold/a7f4d6666dcbcbbe1faf to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"path"
"github.com/fsnotify/fsnotify"
)
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("error from fsnotify.NewWatcher(): %s", err)
}
defer watcher.Close()
// create a temp dir
tempDir, err := ioutil.TempDir("", "fs_notify_bug")
if err != nil {
log.Fatalf("error creating tempdir: %s", err)
}
log.Printf("using tempdir: %s", tempDir)
// create 300 files in temp dir (no watch set yet)
for i := 0; i < 300; i++ {
f := path.Join(tempDir, fmt.Sprintf("file%d", i))
err = ioutil.WriteFile(f, []byte("stuff"), 0644)
if err != nil {
log.Fatal("error creating %s: %s", f, err)
}
log.Printf("created %s", f)
}
err = watcher.Add(tempDir)
if err != nil {
log.Fatalf("error adding %s: %s", tempDir, err)
}
log.Printf("watching %s", tempDir)
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
case err := <-watcher.Errors:
log.Printf("error from <-watcher.Errors: %s", err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment