1 Star 0 Fork 0

butnet / fsnotify

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
watch.go 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
张波 提交于 2022-12-09 17:53 . linux 关闭事件
package main
import "gitee.com/butnet/fsnotify"
// This is the most basic example: it prints events to the terminal as we
// receive them.
func watch(paths ...string) {
if len(paths) < 1 {
exit("must specify at least one path to watch")
}
// Create a new watcher.
w, err := fsnotify.NewWatcher()
if err != nil {
exit("creating a new watcher: %s", err)
}
defer w.Close()
// Start listening for events.
go watchLoop(w)
// Add all paths from the commandline.
for _, p := range paths {
err = w.Add(p)
if err != nil {
exit("%q: %s", p, err)
}
}
printTime("ready; press ^C to exit")
<-make(chan struct{}) // Block forever
}
func watchLoop(w *fsnotify.Watcher) {
i := 0
for {
select {
// Read from Errors.
case err, ok := <-w.Errors:
if !ok { // Channel was closed (i.e. Watcher.Close() was called).
return
}
printTime("ERROR: %s", err)
// Read from Events.
case e, ok := <-w.Events:
if !ok { // Channel was closed (i.e. Watcher.Close() was called).
return
}
// Just print the event nicely aligned, and keep track how many
// events we've seen.
i++
printTime("%3d %s", i, e)
}
}
}
Go
1
https://gitee.com/butnet/fsnotify.git
git@gitee.com:butnet/fsnotify.git
butnet
fsnotify
fsnotify
v1.6.7

搜索帮助

53164aa7 5694891 3bd8fe86 5694891