当前位置:网站首页> Go语言fsnotify接口实现监测文件修改
Go语言fsnotify接口实现监测文件修改
2022-06-27 19:57:00 【1024问】
前言
安装工具
关键类型
Event结构体
Op类型
Watcher结构体
Channel
函数
Watcher工厂函数
完整例子
前言在开发过程中,经常需要观察本地文件系统的更改。经过谷歌了几个小时后,到了一个简单的工具来做这件事。
该工具就是fsnotify是一个Go跨平台文件系统通知工具。它提供了一个简单的接口来监测本地文件系统中的更改。
本文我们就来看看如何使用这个工具。
安装工具$ go get github.com/fsnotify/fsnotify关键类型我们先来了解下fsnotify工具的所有类型。
Event结构体Event结构体表示单个文件系统通知。
函数String()返回一个“file: REMOVE|WRITE|…”格式字符串表示事件的字符串。
type Event struct { Name string //文件或目录的相对路径 Op Op //文件更改事件}func (e Event) String() stringOp类型该工具描述了一组文件操作。它们是可以触发通知的通用文件操作。
type Op uint32const ( Create Op = 1 << iota Write Remove Rename Chmod)Watcher结构体Watcher结构体是该工具的核心。包含两个channel和三个函数。
type Watcher struct { Events chan Event Errors chan error ...}func (w *Watcher) Add(name string) errorfunc (w *Watcher) Remove(name string) error func NewWatcher() (*Watcher, error)ChannelEvents 通道
Errors 通道
函数Add:非递归监测文件或目录的变化。
Remove:停止监测文件或目录。
Close:关闭所有文件或目录的监测以及关闭Events通道。
Watcher工厂函数函数NewWatcher通过底层操作系统调用创建watcher对象,并等待事件通知。
func NewWatcher() (*Watcher, error)完整例子import ( "log" "github.com/fsnotify/fsnotify")func main() { watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal("new watcher failed: ", err) } defer watcher.Close() done := make(chan bool) go func() { defer close(done) for { select { case event, ok := <-watcher.Events: if !ok { return } log.Printf("%s %s\n", event.Name, event.Op) case err, ok := <-watcher.Errors: if !ok { return } log.Println("error: ", err) } } }() err = watcher.Add("./") if err != nil { log.Fatal("add failed:", err) } <-done}上面的代码通过启动一个goroutine在后台监测目录或文件的变化,调用函数watcher.Add("./")添加监测的目录是当前目录,也就是main.go文件所在目录。
运行程序后,如果你修改下当前main.go文件会产生如下结果:
Output
2022/06/09 07:01:15 main.go~ CREATE
2022/06/09 07:01:15 main.go WRITE|CHMOD
2022/06/09 07:01:15 main.go~ CREATE
2022/06/09 07:01:15 main.go CHMOD
2022/06/09 07:01:15 main.go WRITE
2022/06/09 07:01:15 main.go~ REMOVE
2022/06/09 07:01:16 main.go CHMOD
上面的输出过程可以发现,修改文件并保存会触发以下动作:
CREATE动作即临时文件的创建
WRITE写文件动作
CHMOD修改文件属性
REMOVE删除临时文件。
以上就是Go语言fsnotify接口实现监测文件修改的详细内容,更多关于Go fsnotify接口监测文件修改的资料请关注软件开发网其它相关文章!
边栏推荐
- Zabbix6.0升级指南-数据库如何同步升级?
- Passerelle de service pour les microservices
- 爬虫笔记(2)- 解析
- 雪糕还是雪“高”?
- "I make the world cooler" 2022 Huaqing vision R & D product launch was a complete success
- 改善深层神经网络:超参数调试、正则化以及优化(三)- 超参数调试、Batch正则化和程序框架
- 资深猎头团队管理者:面试3000顾问,总结组织出8大共性(茅生)
- 二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
- Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
- Web Worker介绍及使用案例
猜你喜欢

Structured machine learning project (I) - machine learning strategy

《7天学会Go并发编程》第六天 go语言Sync.cond的应用和实现 go实现多线程联合执行

mysql操作入门(四)-----数据排序(升序、降序、多字段排序)

Codeforces Round #717 (Div. 2)

Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~

Codeforces Round #716 (Div. 2)

PE买下一家内衣公司

二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)

Codeforces Round #719 (Div. 3)

渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
随机推荐
CUDA error:out of memory caused by insufficient video memory of 6G graphics card
Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决
average-population-of-each-continent
元气森林的5元有矿之死
Codeforces Round #723 (Div. 2)
Yolov6: the fast and accurate target detection framework is open source
netERR_CONNECTION_REFUSED 解决大全
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
这届考生,报志愿比高考更“拼命”
The karsonzhang/fastadmin addons provided by the system reports an error
九九乘法表——C语言
爬虫笔记(3)-selenium和requests
结构化机器学习项目(二)- 机器学习策略(2)
Is it safe for GF futures to open an account?
Gartner focuses on low code development in China how UNIPRO practices "differentiation"
Macro task and micro task understanding
mysql操作入门(四)-----数据排序(升序、降序、多字段排序)
Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
Ellipsis after SQLite3 statement Solutions for