Created
April 19, 2015 18:29
-
-
Save timshannon/603f92824c5294269797 to your computer and use it in GitHub Desktop.
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" | |
| "log" | |
| "os" | |
| "os/signal" | |
| "gopkg.in/fsnotify.v1" | |
| ) | |
| func init() { | |
| c := make(chan os.Signal, 1) | |
| signal.Notify(c, os.Interrupt) | |
| go func() { | |
| for sig := range c { | |
| if sig == os.Interrupt { | |
| os.Exit(1) | |
| } | |
| } | |
| }() | |
| } | |
| func main() { | |
| //create dir | |
| // start watching | |
| // create child dir | |
| // start watching child | |
| // stop watching child | |
| // delete dir | |
| 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) | |
| } | |
| } | |
| }() | |
| os.Mkdir("parent", 0777) | |
| err = watcher.Add("parent") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| os.Mkdir("parent/child", 0777) | |
| err = watcher.Add("parent/child") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| err = watcher.Remove("parent/child") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| err = os.Remove("parent/child") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Println("Child should be deleted, but it's not because fsnotify is still holding a file handle for it, even though it's not longer being directly watched.") | |
| fmt.Println("Hit ctrl-c to close the program, and the file handle will be released, and the folder will finish getting deleted.") | |
| <-done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment