当前位置:网站首页> 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接口监测文件修改的资料请关注软件开发网其它相关文章!
边栏推荐
- 【微服务】(十六)—— 分布式事务Seata
- OpenSSL programming I: basic concepts
- Common APIs (Methods) for scope -number and string
- Vue+mysql login registration case
- Acwing weekly contest 57- digital operation - (thinking + decomposition of prime factor)
- Consumer finance app user insight in the first quarter of 2022 - a total of 44.79 million people
- Use Fiddler to simulate weak network test (2g/3g)
- 游戏手机平台简单介绍
- Codeforces Round #716 (Div. 2)
- Codeforces Round #721 (Div. 2)
猜你喜欢

Secret script of test case design without leakage -- module test

关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证

月薪3万的狗德培训,是不是一门好生意?

Go language slice vs array panic: runtime error: index out of range problem solving

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

Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移

医美大刀,砍向00后

First knowledge of the second bullet of C language

爬虫笔记(3)-selenium和requests

Crawler notes (2) - parse
随机推荐
Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
Workflow automation low code is the key
The karsonzhang/fastadmin addons provided by the system reports an error
Penetration learning - shooting range chapter -dvwa shooting range detailed introduction (continuous updating - currently only the SQL injection part is updated)
Management system itclub (Part 1)
[cloud based co creation] what is informatization? What is digitalization? What are the connections and differences between the two?
Service gateway of microservices
STM32与RC522简单公交卡系统的设计
【mysql实战】查询语句实战演示
Improving deep neural networks: hyperparametric debugging, regularization and optimization (III) - hyperparametric debugging, batch regularization and program framework
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
average-population-of-each-continent
Which method is called for OSS upload
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
Acwing weekly contest 57- digital operation - (thinking + decomposition of prime factor)
游戏手机平台简单介绍
最虚的华人首富更虚了
2022年第一季度“广州好人”刘磊峰:具有强烈的诚信意识和食品安全意识
average-population-of-each-continent
改善深层神经网络:超参数调试、正则化以及优化(三)- 超参数调试、Batch正则化和程序框架