项目地址:
https://github.com/fsnotify/fsnotify
用法:
package main import ( "fmt" "github.com/fsnotify/fsnotify" ) func main() { watcher, _ := fsnotify.NewWatcher() defer watcher.Close() done := make(chan bool) go func() { for { select { case event, ok := <- watcher.Events: if !ok { return } fmt.Println("事件:", event) if event.Op & fsnotify.Write == fsnotify.Write { fmt.Println("文件名:", event.Name) } case err, ok := <-watcher.Errors: if !ok { return } fmt.Println("出错了:", err) } } }() err := watcher.Add("test.txt") if err != nil { fmt.Println("监控文件/文件夹失败:",err) } <- done } // 输出 事件: "test.txt": WRITE 文件名: test.txt
文档更新时间: 2024-04-20 10:57 作者:lee