fsnotify 文件系统通知
Posted 刘贤松handler
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fsnotify 文件系统通知相关的知识,希望对你有一定的参考价值。
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
以上是关于fsnotify 文件系统通知的主要内容,如果未能解决你的问题,请参考以下文章
[Go] 跨平台文件系统监控工具 fsnotify 应用举例
golang 通过fsnotify监控文件,并通过文件变化重启程序