package main import ( "os" "path/filepath" "gopkg.in/fsnotify.v1" ) func WatchRecursively(root string, changeHandler func(fsnotify.Event)) error { watcher, err := fsnotify.NewWatcher() if err != nil { return err } defer watcher.Close() done := make(chan bool) go func() { for { select { case event, ok := <-watcher.Events: if !ok { return } changeHandler(event) case err, ok := <-watcher.Errors: if !ok { return } Error.Printf("error with watcher: %v", err) } } }() dirs := 0 err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.Name() == ".git" { return filepath.SkipDir } if info.IsDir() { err = watcher.Add(path) dirs++ if err != nil { return err } } return nil }) if err != nil { return err } <-done return nil }