2020年8月6日 星期四

[ 文章收集 ] How to Detect File Changes in Golang

Source From Here
Preface
Go has quickly become one of my favorite languages to write in. I often find my self smiling at how easy Go makes almost every task. Detecting file changes is no exception. Using the filepath package from the standard library along with fsnotify, makes this a fairly simple task.

Watching single files or directories
Out of the box, fsnotify provides all the functionality needed to watch a single file or directory:
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.   
  6.     "github.com/fsnotify/fsnotify"  
  7. )  
  8.   
  9. // main  
  10. func main() {  
  11.   
  12.     // creates a new file watcher  
  13.     watcher, err := fsnotify.NewWatcher()  
  14.     if err != nil {  
  15.         fmt.Println("ERROR", err)  
  16.     }  
  17.     defer watcher.Close()  
  18.   
  19.     //  
  20.     done := make(chan bool)  
  21.   
  22.     //  
  23.     go func() {  
  24.         for {  
  25.             select {  
  26.             // watch for events  
  27.             case event := <-watcher.Events:  
  28.                 fmt.Printf("EVENT! %#v\n", event)  
  29.   
  30.                 // watch for errors  
  31.             case err := <-watcher.Errors:  
  32.                 fmt.Println("ERROR", err)  
  33.             }  
  34.         }  
  35.     }()  
  36.   
  37.     // out of the box fsnotify can watch a single file, or a single directory  
  38.     if err := watcher.Add("C:/tmp/"); err != nil {  
  39.         fmt.Println("ERROR", err)  
  40.     }  
  41.   
  42.     <-done  
  43. }  
Watching nested files and directories
Unfortunately fsnotify doesn’t support recursion, so filepath will have to fill in the gaps:
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.     "os"  
  6.     "path/filepath"  
  7.   
  8.     "github.com/fsnotify/fsnotify"  
  9. )  
  10.   
  11. //  
  12. var watcher *fsnotify.Watcher  
  13.   
  14. // main  
  15. func main() {  
  16.   
  17.     // creates a new file watcher  
  18.     watcher, _ = fsnotify.NewWatcher()  
  19.     defer watcher.Close()  
  20.   
  21.     // starting at the root of the project, walk each file/directory searching for  
  22.     // directories  
  23.     if err := filepath.Walk("C:/tmp", watchDir); err != nil {  
  24.         fmt.Println("ERROR", err)  
  25.     }  
  26.   
  27.     //  
  28.     done := make(chan bool)  
  29.   
  30.     //  
  31.     go func() {  
  32.         for {  
  33.             select {  
  34.             // watch for events  
  35.             case event := <-watcher.Events:  
  36.                 fmt.Printf("EVENT! %#v\n", event)  
  37.   
  38.                 // watch for errors  
  39.             case err := <-watcher.Errors:  
  40.                 fmt.Println("ERROR", err)  
  41.             }  
  42.         }  
  43.     }()  
  44.   
  45.     <-done  
  46. }  
  47.   
  48. // watchDir gets run as a walk func, searching for directories to add watchers to  
  49. func watchDir(path string, fi os.FileInfo, err error) error {  
  50.   
  51.     // since fsnotify can watch all the files in a directory, watchers only need  
  52.     // to be added to each nested directory  
  53.     if fi.Mode().IsDir() {  
  54.         return watcher.Add(path)  
  55.     }  
  56.   
  57.     return nil  
  58. }  
Since fsnotify is able to watch all the files in a directory, filepath is used to walk the entire document tree looking only for directories to attach watchers to.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...