import "github.com/fsnotify/fsnotify"
Package fsnotify provides a platform-independent interface for file system notifications.
fsnotify.go inotify.go inotify_poller.go
Common errors that can be reported by a watcher
type Event struct {
Name string // Relative path to the file or directory.
Op Op // File operation that triggered the event.
}Event represents a single file system notification.
String returns a string representation of the event in the form "file: REMOVE|WRITE|..."
Op describes a set of file operations.
These are the generalized file operations that can trigger a notification.
type Watcher struct {
Events chan Event
Errors chan error
// contains filtered or unexported fields
}Watcher watches a set of files, delivering events to a channel.
NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
Code:
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
err = watcher.Add("/tmp/foo")
if err != nil {
log.Fatal(err)
}
<-done
Add starts watching the named file or directory (non-recursively).
Close removes all watches and closes the events channel.
Remove stops watching the named file or directory (non-recursively).
Package fsnotify imports 10 packages (graph) and is imported by 714 packages. Updated 2018-08-30. Refresh now. Tools for package owners.