-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
77 lines (73 loc) · 1.63 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"log"
"strings"
"github.com/fsnotify/fsnotify"
)
func main() {
var array_or_slice = []string{"aaa", "bbb", "ccc"}
a := fmt.Sprint(array_or_slice)
b := strings.Trim(a, "[]")
c := strings.Replace(b, " ", ",", -1)
fmt.Println("a:", a)
fmt.Println("b:", b)
fmt.Println("c:", c)
return
//我们另启一个goroutine来处理监控对象的事件
go func() {
//创建一个监控对象
watch, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watch.Close()
//添加要监控的对象,文件或文件夹
err = watch.Add("./")
if err != nil {
log.Fatal(err)
}
for {
select {
case ev := <-watch.Events:
{
//判断事件发生的类型,如下5种
// Create 创建
// Write 写入
// Remove 删除
// Rename 重命名
// Chmod 修改权限
if ev.Op&fsnotify.Create == fsnotify.Create {
log.Println("创建文件 : ", ev.Name)
}
if ev.Op&fsnotify.Write == fsnotify.Write {
switch ev.Name {
case "homey.yml":
fmt.Println("edit homey")
case "feidan.yml":
fmt.Println("edit feidan")
default:
fmt.Println("edit other:", ev.Name)
}
}
if ev.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("删除文件 : ", ev.Name)
}
if ev.Op&fsnotify.Rename == fsnotify.Rename {
log.Println("重命名文件 : ", ev.Name)
}
if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
log.Println("修改权限 : ", ev.Name)
}
}
case err := <-watch.Errors:
{
log.Println("error : ", err)
return
}
}
}
}()
//循环
select {}
}