Skip to content

Instantly share code, notes, and snippets.

@davecheney
Created August 13, 2012 01:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davecheney/3336284 to your computer and use it in GitHub Desktop.
Save davecheney/3336284 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/howeyc/fsnotify"
"log"
)
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.Watch("/tmp")
if err != nil {
log.Fatal(err)
}
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}
@larzconwell
Copy link

This is what I'm doing now for my program github.com/larzconwell/gwatch

The example shows this though:

watcher, err := fsnotify.NewWatcher()
    if err != nil {
        log.Fatal(err)
    }

    // Process events
    go func() {
        for {
            // Everything below seems to be ignored for some reason.
            select {
            case ev := <-watcher.Event:
                log.Println("event:", ev)
            case err := <-watcher.Error:
                log.Println("error:", err)
            }
        }
    }()

    err = watcher.Watch("/tmp")
    if err != nil {
        log.Fatal(err)
    }

@larzconwell
Copy link

Here's the full code including main, there may be an error, I couldn't test it as I'm reinstalling my system and don't have a compiler at hand.

package main

import (
  "log"
  "github.com/howeyc/fsnotify"
)

func main() {
  watcher, err := fsnotify.NewWatcher()
  if err != nil {
    log.Fatal(err)
  }

  go func() {
    for {
      // For some reason this select statement
      // is ignored
      select {
      case ev := <-watcher.Event:
        log.Println(ev)
      case err := <-watcher.Error:
        log.Println(err)
      }
    }
  }()

  if err = watcher.Watch("some-file-here"); err != nil {
    log.Fatal(err)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment