Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Watch ¶
type Watch struct {
// contains filtered or unexported fields
}
Watch watches over a directory and it's sub-directories, recursively. Also watches files, if the path is explicitly provided. If a path does no longer exists, it will be removed.
func New ¶
New creates a new *Watch
Example ¶
// prepare sample home directory to watch over rootDirectory, err := ioutil.TempDir("", "dirwatch-example") if err != nil { panic(err) } os.RemoveAll(rootDirectory) os.Mkdir(rootDirectory, 0777) // our notification callback (I feel it's simpler to // have a callback instead of passing a channel in an API) var events = make(chan fsnotify.Event, 100) notify := func(ev fsnotify.Event) { events <- ev } // create the watcher watcher := New(notify, rootDirectory) defer watcher.Stop() // creating a directory inside the root/home dir2 := filepath.Join(rootDirectory, "lab2") os.Remove(dir2) err = os.Mkdir(dir2, 0777) if err != nil { panic(err) } <-time.After(time.Millisecond * 100) select { case ev := <-events: if strings.Contains(ev.Name, "dirwatch-example") && strings.Contains(ev.Name, "lab2") && ev.Op == fsnotify.Create { fmt.Println("OK") } case <-time.After(time.Second * 3): }
Output: OK
Click to show internal directories.
Click to hide internal directories.