Skip to content

Instantly share code, notes, and snippets.

@kavu
Created April 6, 2014 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kavu/10010182 to your computer and use it in GitHub Desktop.
Save kavu/10010182 to your computer and use it in GitHub Desktop.
One day @gamesbrainiac asked me to make a simple File Watcher on Go…
package main
import (
"log"
"os"
"path/filepath"
"time"
)
type pathsModTimes map[string]time.Time
var paths pathsModTimes
func walker(path string, info os.FileInfo, err error) error {
var modTime time.Time = info.ModTime()
if err != nil {
panic(err)
}
if t, ok := paths[path]; ok {
if !modTime.Equal(t) {
log.Printf("Something happened with `%s`\n", path)
paths[path] = modTime
}
} else {
paths[path] = modTime
}
return nil
}
func watcher(path string) {
for {
select {
case <-time.After(1 * time.Second):
filepath.Walk(path, walker)
}
}
}
func init() {
paths = make(pathsModTimes)
}
func main() {
go watcher("data")
for {
select {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment