This commit is contained in:
2024-06-22 05:13:30 +00:00
parent 8e449f25a8
commit 193b121893
4 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package main
import (
"context"
"log"
"os"
"os/signal"
"github.com/fsnotify/fsnotify"
)
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Has(fsnotify.Create) {
log.Println("created file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
case <-ctx.Done():
return
}
}
}()
if err = watcher.Add("/home/yono/watchdir"); err != nil {
log.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
cancel()
}