Some month ago, I started a new API project in Golang, and I found the edit-compile-start-kill loop not pleasant. I start looking for a better way to just edit and test my project.

I would like a dead simple option, and I found this solution. I updated it for only restarting on "write" events.

The makefile I add on my project now:

PID      = ./app.pid
GO_FILES = $(wildcard *.go)
APP      = ./app

serve: start
    @fswatch -x -o --event Created --event Updated --event Renamed -r -e '.*' -i '\.go$$'  . | xargs -n1 -I{}  make restart || make kill

kill:
    @kill `cat $(PID)` || true

before:
    @echo "actually do nothing"

build: $(GO_FILES)
    @go build -o $(APP)

start:
    @sh -c "$(APP) & echo $$! > $(PID)"

restart: kill before build start

.PHONY: start serve restart kill before # let's go to reserve rules names

Note: On Mac, you need to install fswatch brew install fswatch