genlong %!s(int64=2) %!d(string=hai) anos
achega
b0ebb5ccdb
Modificáronse 3 ficheiros con 68 adicións e 0 borrados
  1. 7 0
      go.mod
  2. 4 0
      go.sum
  3. 57 0
      main.go

+ 7 - 0
go.mod

@@ -0,0 +1,7 @@
+module mysteel_watch
+
+go 1.18
+
+require github.com/fsnotify/fsnotify v1.5.4
+
+require golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect

+ 4 - 0
go.sum

@@ -0,0 +1,4 @@
+github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
+github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
+golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
+golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

+ 57 - 0
main.go

@@ -0,0 +1,57 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"strings"
+	"time"
+
+	"github.com/fsnotify/fsnotify"
+)
+
+func main() {
+	ListenFolderNew()
+}
+
+func ListenFolderNew() {
+	fmt.Print("-----文件夹监听-------")
+	watcher, err := fsnotify.NewWatcher()
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer watcher.Close()
+
+	done2 := make(chan bool)
+	go func() {
+		for {
+			select {
+			case event, ok := <-watcher.Events:
+				fmt.Println("start event")
+				fmt.Println(event, ok)
+				fmt.Println("end event")
+				if !ok {
+					return
+				}
+				fmt.Println("event.Op=>%#v", event.Op)
+				fmt.Println("文件操作类型判断是不是新建一个文件:%#v", event.Op&fsnotify.Create == fsnotify.Create)
+				if event.Op&fsnotify.Create == fsnotify.Create {
+					fmt.Println("*Create**event")
+					fmt.Println("新的文件:", event.Name)
+					mate := strings.Split(event.Name, "\\")
+					fileName := mate[len(mate)-1]
+					fmt.Println("fileName:", fileName)
+				}
+			case err := <-watcher.Errors:
+				log.Println("error:", err)
+			case <-time.After(60 * time.Second):
+				continue
+			}
+
+		}
+	}()
+	err = watcher.Add("E:/files")
+	if err != nil {
+		log.Fatal(err)
+	}
+	<-done2
+}