Created
March 26, 2016 18:36
-
-
Save armhold/a7f4d6666dcbcbbe1faf 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" | |
"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