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{})
}
相关推荐
上去我就QWER1 小时前
Qt中如何获取系统版本信息
开发语言·qt
我是苏苏2 小时前
C#高级:程序查询写法性能优化提升策略(附带Gzip算法示例)
开发语言·算法·c#
木木子99992 小时前
业务架构、应用架构、数据架构、技术架构
java·开发语言·架构
鬼火儿6 小时前
SpringBoot】Spring Boot 项目的打包配置
java·后端
cr7xin7 小时前
缓存三大问题及解决方案
redis·后端·缓存
大佬,救命!!!7 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
赵文宇(温玉)8 小时前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
qq7422349848 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
Joker100858 小时前
仓颉自定义序列化:从原理到高性能多协议实现
开发语言
Adellle8 小时前
2.单例模式
java·开发语言·单例模式