当前位置:网站首页> 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接口监测文件修改的资料请关注软件开发网其它相关文章!
边栏推荐
- How many ways does selenium upload files? I don't believe you have me all!
- Fill in the blank of rich text test
- Ellipsis after SQLite3 statement Solutions for
- Is it safe for GF futures to open an account?
- Penetration learning - shooting range chapter -dvwa shooting range detailed introduction (continuous updating - currently only the SQL injection part is updated)
- 结构化机器学习项目(一)- 机器学习策略
- 个人TREE ALV 模版-加快你的开发
- Go language slice vs array panic: runtime error: index out of range problem solving
- 01 golang environment construction
- 微服務之服務網關
猜你喜欢

Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?

DCC888 :Register Allocation

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

Consumer finance app user insight in the first quarter of 2022 - a total of 44.79 million people

Workflow automation low code is the key

How to use RPA to achieve automatic customer acquisition?

《7天學會Go並發編程》第7天 go語言並發編程Atomic原子實戰操作含ABA問題
![[microservices] (16) -- distributed transaction Seata](/img/1b/aeb534d5a0bd40f5fc14e64bdf5aa9.png)
[microservices] (16) -- distributed transaction Seata

MySQL数据库 实验报告(一)

最虚的华人首富更虚了
随机推荐
Basics of operators
月薪3万的狗德培训,是不是一门好生意?
Where can I set the slides on the front page of CMS applet?
ABAP随笔-关于ECC后台server读取Excel方案的想法
netERR_ CONNECTION_ Refused solution
Crontab scheduled task common commands
Acwing weekly contest 57- digital operation - (thinking + decomposition of prime factor)
Structured machine learning project (I) - machine learning strategy
Typescript learning
Crawler notes (1) - urllib
PHP connects to database to realize user registration and login function
Which method is called for OSS upload
Learn to go concurrent programming in 7 days go language sync Application and implementation of cond
结构化机器学习项目(二)- 机器学习策略(2)
99 multiplication table - C language
Improving deep neural networks: hyperparametric debugging, regularization and optimization (III) - hyperparametric debugging, batch regularization and program framework
OpenSSL Programming II: building CA
Character interception triplets of data warehouse: substrb, substr, substring
【mysql实战】查询语句实战演示
[microservices] (16) -- distributed transaction Seata