当前位置:网站首页> 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接口监测文件修改的资料请关注软件开发网其它相关文章!
边栏推荐
- Crawler notes (1) - urllib
- Day8 - cloud information project introduction and creation
- 移动端避免使用100vh[通俗易懂]
- 渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
- Consumer finance app user insight in the first quarter of 2022 - a total of 44.79 million people
- 九九乘法表——C语言
- Common APIs (Methods) for scope -number and string
- 二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
- This set of steps for performance testing using JMeter includes two salary increases and one promotion
- CUDA error:out of memory caused by insufficient video memory of 6G graphics card
猜你喜欢

爬虫笔记(3)-selenium和requests

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

Crawler notes (2) - parse

Yolov6: the fast and accurate target detection framework is open source

Service gateway of microservices

STM32与RC522简单公交卡系统的设计

渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)

Codeforces Round #723 (Div. 2)

Educational Codeforces Round 108 (Rated for Div. 2)

Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
随机推荐
雪糕还是雪“高”?
How to use RPA to achieve automatic customer acquisition?
mysql操作入门(四)-----数据排序(升序、降序、多字段排序)
Introduction to MySQL operation (IV) -- data sorting (ascending, descending, and multi field sorting)
Introduction to ARCS Model
Character interception triplets of data warehouse: substrb, substr, substring
初识C语言 第二弹
Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
6G显卡显存不足出现CUDA Error:out of memory解决办法
Passerelle de service pour les microservices
结构化机器学习项目(一)- 机器学习策略
Transformation from student to engineer
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
广告太「野」,吉野家「渡劫」
Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
九九乘法表——C语言
Crawler notes (2) - parse
"I make the world cooler" 2022 Huaqing vision R & D product launch was a complete success
Conversion between flat array and JSON tree