github.com/gogf/gf@v1.16.9/.example/os/gfsnotify/gfsnotify_callback.go (about)

     1  package main
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/gogf/gf/os/gfsnotify"
     7  	"github.com/gogf/gf/os/glog"
     8  	"github.com/gogf/gf/os/gtimer"
     9  )
    10  
    11  func main() {
    12  	c1, err := gfsnotify.Add("/home/john/temp/log", func(event *gfsnotify.Event) {
    13  		glog.Println("callback1")
    14  	})
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  	c2, err := gfsnotify.Add("/home/john/temp/log", func(event *gfsnotify.Event) {
    19  		glog.Println("callback2")
    20  	})
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	// 5秒后移除c1的回调函数注册,仅剩c2
    25  	gtimer.SetTimeout(5*time.Second, func() {
    26  		gfsnotify.RemoveCallback(c1.Id)
    27  		glog.Println("remove callback c1")
    28  	})
    29  	// 10秒后移除c2的回调函数注册,所有的回调都移除,不再有任何打印信息输出
    30  	gtimer.SetTimeout(10*time.Second, func() {
    31  		gfsnotify.RemoveCallback(c2.Id)
    32  		glog.Println("remove callback c2")
    33  	})
    34  
    35  	select {}
    36  
    37  }