英文:
How is recwatch supposed to work?
问题
我正在尝试让recwatch工作。不过,我对它的接口感到困惑。是的,我可以创建一个监视器并向其中添加文件夹,但似乎没有一种方法可以启动一个事件循环,以便我可以接收通知。
在原始代码中,有一个Run
接收器专门用于此目的。
我有什么遗漏吗?
英文:
I'm trying to get recwatch to work. I'm confused by its interface, though. Yes, I can make a watcher and add folders to it, but there does not seem to be a way to start an event loop that will allow me to receive notifications.
In the original code, there was a Run
receiver for just this purpose.
Am I missing something?
答案1
得分: 1
观察器在创建后立即开始发出事件。只需要从RecursiveWatcher.Watcher.Events
中读取它们。示例代码如下:
package main
import (
"fmt"
"github.com/xyproto/recwatch"
)
func main() {
w, err := recwatch.NewRecursiveWatcher("sample_dir")
if err != nil {
panic(err)
}
for {
select {
case event := <-w.Events:
fmt.Printf("事件: %s\n", event)
case event := <-w.Errors:
fmt.Printf("错误: %s\n", event)
}
}
}
英文:
The watcher starts emitting events as soon as it is created. All that's required is to read them from RecursiveWatcher.Watcher.Events
. Example:
package main
import (
"fmt"
"github.com/xyproto/recwatch"
)
func main() {
w, err := recwatch.NewRecursiveWatcher("sample_dir")
if err != nil {
panic(err)
}
for {
select {
case event := <-w.Events:
fmt.Printf("Event: %s\n", event)
case event := <-w.Errors:
fmt.Printf("Error: %s\n", event)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论