Recwatch是如何工作的?

huangapple go评论80阅读模式
英文:

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 (
	&quot;fmt&quot;

	&quot;github.com/xyproto/recwatch&quot;
)

func main() {
	w, err := recwatch.NewRecursiveWatcher(&quot;sample_dir&quot;)
	if err != nil {
		panic(err)
	}
	for {
		select {
		case event := &lt;-w.Events:
			fmt.Printf(&quot;Event: %s\n&quot;, event)
		case event := &lt;-w.Errors:
			fmt.Printf(&quot;Error: %s\n&quot;, event)
		}
	}
}

huangapple
  • 本文由 发表于 2016年3月24日 08:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/36191422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定