当前位置:网站首页>Fsnotify interface of go language to monitor file modification
Fsnotify interface of go language to monitor file modification
2022-06-27 22:53:00 【1024 Q】
Preface
Installation tools
Key types
Event Structure
Op type
Watcher Structure
Channel
function
Watcher Factory function
Complete example
PrefaceIn the development process , It is often necessary to observe changes in the local file system . After Google for a few hours , To a simple tool to do this .
The tool is fsnotify It's a Go Cross platform file system notification tool . It provides a simple interface to monitor changes in the local file system .
In this article, we will take a look at how to use this tool .
Installation tools$ go get github.com/fsnotify/fsnotify Key types Let's get to know fsnotify All types of tools .
Event StructureEvent The struct represents a single file system notification .
function String() Return to one “file: REMOVE|WRITE|…” The format string represents the string of the event .
type Event struct { Name string // The relative path of a file or directory Op Op // File change event }func (e Event) String() stringOp type This tool describes a set of file operations . They are generic file operations that trigger notifications .
type Op uint32const ( Create Op = 1 << iota Write Remove Rename Chmod)Watcher Structure Watcher The structure is the core of the tool . Contains two channel And three functions .
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 passageway
Errors passageway
functionAdd: Non recursive monitoring of file or directory changes .
Remove: Stop monitoring files or directories .
Close: Turn off the monitoring of all files or directories and close Events passageway .
Watcher Factory functionfunction NewWatcher Create by calling the underlying operating system watcher object , And wait for the event notification .
func NewWatcher() (*Watcher, error) Complete example 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} The above code starts a goroutine Monitoring changes in directories or files in the background , Call function watcher.Add("./") The directory added for monitoring is the current directory , That is to say main.go File directory .
After running the program , If you modify the current main.go The file will produce the following results :
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
The output process above shows that , Modifying the file and saving it will trigger the following actions :
CREATE Action is the creation of temporary files
WRITE Write file action
CHMOD Modify file properties
REMOVE Delete temporary files .
That's all Go Language fsnotify Interface implementation monitors the details of file modification , More about Go fsnotify For information on modification of interface monitoring files, please pay attention to other relevant articles of software development network !
边栏推荐
- Arcgis-engine二次开发之空间关系查询与按图形查询
- Personal tree ALV template - accelerate your development
- 爬虫笔记(3)-selenium和requests
- average-population-of-each-continent
- The karsonzhang/fastadmin addons provided by the system reports an error
- Azure Kinect DK 实现三维重建 (jetson实时版)
- Which method is called for OSS upload
- First knowledge of the second bullet of C language
- 初识C语言 第二弹
- MySQL greater than less than or equal to symbol representation
猜你喜欢

广告太「野」,吉野家「渡劫」

Spatial relation query and graph based query in secondary development of ArcGIS Engine

Livox Lidar+海康Camera实时生成彩色点云

Arcgis-engine二次开发之空间关系查询与按图形查询

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

Management system itclub (medium)

《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题

Penetration learning - shooting range chapter -dvwa shooting range detailed introduction (continuous updating - currently only the SQL injection part is updated)

Memoirs of actual combat: breaking the border from webshell

C # QR code generation and recognition, removing white edges and any color
随机推荐
PHP连接数据库实现用户注册登录功能
ABAP essay-excel-3-batch import (breaking through 9999 lines of standard functions)
Penetration learning - shooting range chapter -dvwa shooting range detailed introduction (continuous updating - currently only the SQL injection part is updated)
关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证
Typescript learning
Crawler notes (3) -selenium and requests
Spatial relation query and graph based query in secondary development of ArcGIS Engine
Crawler notes (2) - parse
Azure Kinect DK realizes 3D reconstruction (Jetson real-time version)
Basic data type and complex data type
Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
[suggested collection] ABAP essays-excel-4-batch import recommended
改善深层神经网络:超参数调试、正则化以及优化(三)- 超参数调试、Batch正则化和程序框架
初识C语言 第二弹
99 multiplication table - C language
Where can I set the slides on the front page of CMS applet?
One to many association in MySQL to obtain the latest data in multiple tables
How to prioritize the contents in the queue every second
Go语言fsnotify接口实现监测文件修改
Avoid using 100vh[easy to understand] at mobile terminal