File system notifications for Go
fsnotify utilizes golang.org/x/sys rather than syscall from the standard library.
跨平台:Windows、Linux、BSD 和 macOS。
Adapter | OS | Status |
---|---|---|
inotify | Linux 2.6.27 or later, Android* | Supported |
kqueue | BSD, macOS, iOS* | Supported |
ReadDirectoryChangesW | Windows | Supported |
FSEvents | macOS | Planned |
FEN | Solaris 11 | In Progress |
fanotify | Linux 2.6.37+ | Maybe |
USN Journals | Windows | Maybe |
Polling | All | Maybe |
* Android and iOS are untested.
Please see the documentation and consult the FAQ for usage information.
package main
import (
"log"
"github.com/fsnotify/fsnotify"
)
func main() {
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
}