A simple Go file watcher that will stop & restart your main()
function on file changes.
I just want to stop my server, and run it again every time I save a file.
go install marwan.io/gowatch@latest
from your main app directory, run gowatch
it reads your current working directory and runs the two typical commands:
-
go build
-
./<name-of-compiled-binary>
So this only works in main
packages.
Also, this ignores your vendor
folder & your _test.go
files.
Q: Why doesn't it just run go run main.go
?
A: go run
does the same thing as go build
, but calls the resulting binary as a subprocess. This makes it harder to reach stdout
and killing the main.go
process, won't necessarily kill the subprocess, so you end up trying to run the server twice (which ends up with "port is already taken" kind of error).
Q: How does it compare with other tools?
A: I haven't tried most of them, but I wanted to make this as simple as just running one command to get what I'm looking for without having to turn and twist a lot of knobs.