当前位置:网站首页> 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接口监测文件修改的资料请关注软件开发网其它相关文章!
边栏推荐
猜你喜欢

Use Fiddler to simulate weak network test (2g/3g)

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

【微服务】(十六)—— 分布式事务Seata

DCC888 :Register Allocation
Conversion between flat array and JSON tree

Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)

CUDA error:out of memory caused by insufficient video memory of 6G graphics card

微服務之服務網關

Kill the general and seize the "pointer" (Part 2)

雪糕还是雪“高”?
随机推荐
Golang uses regularity to match substring functions
average-population-of-each-continent
Gartner focuses on low code development in China how UNIPRO practices "differentiation"
关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证
OData - SAP S4 OP 中使用SAP API Hub 的API
gomock mockgen : unknown embedded interface
Passerelle de service pour les microservices
CUDA error:out of memory caused by insufficient video memory of 6G graphics card
Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
How to use RPA to achieve automatic customer acquisition?
PHP connects to database to realize user registration and login function
Which method is called for OSS upload
Codeforces Round #716 (Div. 2)
「R」使用ggpolar绘制生存关联网络图
2022年第一季度“广州好人”刘磊峰:具有强烈的诚信意识和食品安全意识
This set of steps for performance testing using JMeter includes two salary increases and one promotion
PCIe knowledge point -008: structure of PCIe switch
改善深层神经网络:超参数调试、正则化以及优化(三)- 超参数调试、Batch正则化和程序框架
最虚的华人首富更虚了