A simple Go program that watches Go files in a directory and restarts the program when a change is detected. It runs the go run
command to start the program.
It only works on macOS and linux, and maybe even BSD but I haven't tested it.
- macOS: uses macOS's built-in
kqueue()
andkevent()
system calls. You can read Apple's docs about them. - linux: uses linux's
inotify
API. You can read Linux man pages about it.
This is not a production-ready program. It's just a fun project to learn about filesystem events in Go (macOS and linux).
Usage: gomon <file/dir>
Watches the directory for changes and restarts the program when a change is detected.
- file: When a file is specified, the program will watch all .go files in that file's parent directory.
- dir: When a directory is specified, the program will watch all .go files in the directory.
Clone the repository.
Run the following command to build the program:
make build
To install gomon to /usr/local/bin, run:
make install
To rebuild and reinstall, use:
make update
To remove gomon from /usr/local/bin, run:
make uninstall
Install Linux. Thanks.
Contributions are welcome!
- main.go is the entry point of the program. It
- instantiates a
ProcessManager
struct. - calls
RunProcess()
to start the Go program. - calls
Watch()
to watch the directory for changes.
- instantiates a
- process_manager.go containers a
ProcessManager
struct that is used to start and stop the Go program.RunProcess()
starts the Go program withgo run
command.StopProcess()
stops the Go program.
- watcher.go contains a
Watch()
function that watches all the Go files in the directory and callsonChange
when a change is detected. It is actually a wrapper around platform specificwatch()
function. Check kqueue.go and inotify.go for the implementations.
- Use
log.Println()
instead offmt.Println()
. - Use conventional commit messages.