Last active
June 1, 2022 15:23
-
-
Save mengstr/e6a672c1261d72ef2c8723431aa6f9f1 to your computer and use it in GitHub Desktop.
golang auto-restart app during development
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
// "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