golang fsnotify使用

Go 复制代码
package main

import (
	"log"

	"github.com/fsnotify/fsnotify"
)

func main() {
	// Create new watcher.
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	// Start listening for events.
	go func() {
		for {
			select {
			case event, ok := <-watcher.Events:
				if !ok {
					return
				}
				log.Println("event:", event)
				if event.Op == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err, ok := <-watcher.Errors:
				if !ok {
					return
				}
				log.Println("error:", err)
			}
		}
	}()

	// Add a path.
	err = watcher.Add("/Users/cobainzheng/tmp")
	if err != nil {
		log.Fatal(err)
	}

	// Block main goroutine forever.
	<-make(chan struct{})
}
相关推荐
葫芦和十三3 小时前
图解 MongoDB 13|WiredTiger 存储引擎:B-tree、页和 checkpoint 三件套
后端·mongodb·agent
葫芦和十三3 小时前
图解 MongoDB 14|Cache 与淘汰:WiredTiger 的内存治理
后端·mongodb·面试
IT_陈寒7 小时前
Vue这个坑我跳了两次,原来问题出在这
前端·人工智能·后端
ServBay8 小时前
9 个 Python 第三方库推荐,不用 AI 都好像多出一个团队
后端·python
用户8356290780518 小时前
如何使用 Python 添加和管理 Excel 批注(完整示例)
后端·python
用户8356290780518 小时前
使用 Python 管理 Excel 工作表:创建、复制、删除与重命名
后端·python
lizhongxuan8 小时前
Agent Tool
后端
CaffeinePro8 小时前
依赖注入:FastAPI最核心的解耦能力案例解析
后端·fastapi
Assby9 小时前
从 Function Calling 到 MCP:理解 Agent 工具调用的底层通信机制
人工智能·后端
打字机v9 小时前
创建第一个spring-boot项目
后端