This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Log" "Github.com/go-fsnotify/fsnotify") func main () {watcher, err: = Fsnotify. Newwatcher () if err! = Nil {log. Fatal (Err)}defer watcher. Close () Done: = Make (chan bool) go func () {for {select {case event: = <-watcher. Events:log. Println ("Event:", event) if event. Op&fsnotify. Write = = Fsnotify. Write {log. PRINTLN ("Modified file:", event.) Name)}case Err: = <-watcher. Errors:log. Println ("Error:", Err)}}} () Err = Watcher. ADD ("slog") if err! = Nil {log. Fatal (err)}<-done}
Package Mainimport ("github.com/howeyc/fsnotify" "Log") type monitor struct {watch *fsnotify. Watcher}func Main () {M, err: = Newmonitor () if err! = Nil {log. PRINTLN (Err) return}m.do () M.watch.watch (".. /") Select {}}func newmonitor () (monitor, error) {Mon, err: = Fsnotify. Newwatcher () return Monitor{mon}, Err}func (Self Monitor) does () {go func () {for {select * {case w: = <-self.watch.event:log . Println (W) if W.ismodify () {continue}if w.isdelete () {log. Println ("File is deleted.") Continue}if W.isrename () {w = <-self.watch.eventlog.println (w) self.watch.RemoveWatch (w.name) log. Println (W.name, "renamed.")} Case ERR: = <-self.watch.error:log. Fatalln (Err)}}} ()}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.