Created
April 14, 2014 07:20
-
-
Save jouyouyun/10623782 to your computer and use it in GitHub Desktop.
Test fsnotify for watching dir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/howeyc/fsnotify" | |
"regexp" | |
) | |
func main() { | |
watcher, err := fsnotify.NewWatcher() | |
if err != nil { | |
fmt.Println("New Watch Failed: ", err) | |
return | |
} | |
if err := watcher.Watch("/tmp/Test"); err != nil { | |
fmt.Println("Watch dir failed: ", err) | |
return | |
} | |
go func() { | |
defer watcher.Close() | |
for { | |
select { | |
case ev := <-watcher.Event: | |
ok, _ := regexp.MatchString(".swp$", ev.Name) | |
if !ok { | |
ok, _ = regexp.MatchString(".swpx$", ev.Name) | |
if !ok { | |
fmt.Println("Watch Event: ", ev) | |
} | |
} | |
case err := <-watcher.Error: | |
fmt.Println("Watch Event Error: ", err) | |
} | |
} | |
}() | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"\.swpx?$"