当前位置:网站首页>go --- 监控文件变化
go --- 监控文件变化
2022-06-12 20:18:00 【谬也】
在对文件进行热更新时,需要监听文件的变化,记录下go中实现文件监听的一种方式。
go代码示例:
当文件被重命名或删除时,执行更新操作(此处为打印delete)
package main
import (
"fmt"
"github.com/fsnotify/fsnotify"
)
func main(){
watcher, _ := fsnotify.NewWatcher()
watcher.Add("test.txt")
defer watcher.Close();
select {
case event := <-watcher.Events:
{
if event.Op&fsnotify.Rename == fsnotify.Rename || event.Op&fsnotify.Remove == fsnotify.Remove {
fmt.Println("delete")
} else if event.Op&fsnotify.Write == fsnotify.Write {
} else if event.Op&fsnotify.Create == fsnotify.Create {
}
}
case err := <-watcher.Errors:
fmt.Println(err.Error())
}
}
如有不对,烦请指出,感谢~
边栏推荐
猜你喜欢
A simple understanding of consistent hash
Axure RP 9 for MAC (interactive product prototyping tool) Chinese version
1. Getting to know R
Wechat jsapi payment pit summary
system()
一致性哈希的简单认识
Wall Street cheat sheet
PostgreSQL database replication - background first-class citizen process walreceiver PG_ stat_ wal_ Receiver view
QT pro file configuration ffmpeg macro
BigTable (II): how BigTable achieves scalability and high performance
随机推荐
进程会计、进程时间、守护进程
Blue Bridge Cup basic-15 VIP question string comparison
unable to recognize “*.yaml“: no matches for kind “ClusterRole“ in version “rbac.authoriz
20 shortcut keys for vs code!
【生成对抗网络学习 其三】BiGAN论文阅读笔记及其原理理解
What does MySQL full value match mean
golang类型断言理解[go语言圣经]
JDBC interface summary
Unsupported class file major version 60
House raiding 3
PostgreSQL database replication - background first-class citizen process walreceiver PG_ stat_ wal_ Receiver view
Niuke net: somme des trois nombres
Interpreter Files
Alipay payment episode 11: monitoring after successful payment callback
QT知识:Qt Widgets小部件类【01】
Test prerequisites: recommend a special cross platform app performance test tool!
P5076 [deep base 16. Example 7] common binary tree (simplified version)
Understanding of data in memory
Axure RP 9 for MAC (interactive product prototyping tool) Chinese version
In 2022, FISCO bcos MVP recognized that the channel was open and invited you to become an open source leader