Skip to content

Instantly share code, notes, and snippets.

@mengstr
Last active June 1, 2022 15:23
Show Gist options
  • Save mengstr/e6a672c1261d72ef2c8723431aa6f9f1 to your computer and use it in GitHub Desktop.
Save mengstr/e6a672c1261d72ef2c8723431aa6f9f1 to your computer and use it in GitHub Desktop.
golang auto-restart app during development
// "github.com/fsnotify/fsnotify"
watcher, err := fsnotify.NewWatcher()
defer watcher.Close()
go func() {
for {
select {
case event, _ := <-watcher.Events:
fmt.Printf("PID %d, Event %v\n", os.Getpid(), event)
if event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Write == fsnotify.Write {
if strings.Contains(event.Name, ".go") || strings.Contains(event.Name, ".png") ||
strings.Contains(event.Name, ".svg") || strings.Contains(event.Name, ".css") ||
strings.Contains(event.Name, ".html") {
fmt.Printf("PID %d, Event %v\n", os.Getpid(), event)
os.Exit(0)
}
}
}
}
}()
watcher.Add(".")
watcher.Add("assets/images")
watcher.Add("assets/styles")
watcher.Add("assets/js")
watcher.Add("assets/templates/html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment