1 Star 19 Fork 4

草原上有什么 / Hummingbird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
filewatch.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
shen 提交于 2022-09-04 16:43 . 上传
package main
import (
"fmt"
"log"
"time"
"utils/ulog"
"github.com/fsnotify/fsnotify"
)
var JScodeDATA chan struct{} = make(chan struct{}, 1) //js代码内存,用于加速读取js代码
var watcherFile string
var timeonce bool
func Filewatch() {
var path string
if con.Jscode.Loadfromfile == "" {
path = "Hconfig.xml"
} else {
path = con.Jscode.Loadfromfile
}
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
}
if event.Op&fsnotify.Write == fsnotify.Write {
if timeonce == false {
time.AfterFunc(2*time.Second, func() {
JScodeDATA <- struct{}{}
timeonce = false
})
timeonce = true
}
fmt.Println("writing", time.Now())
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("Remove file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
err = watcher.Add(path)
if err != nil {
ulog.Println(err)
}
<-done
}
1
https://gitee.com/QdbcShen/hummingbird.git
git@gitee.com:QdbcShen/hummingbird.git
QdbcShen
hummingbird
Hummingbird
master

搜索帮助