Live code reloading for Golang web projects in 19 lines

PID      = /tmp/awesome-golang-project.pid
GO_FILES = $(wildcard *.go)
APP = ./app
serve: restart
@fswatch -o . | xargs -n1 -I{} make restart || make kill

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

before:
@echo "TODO: inject you hooks here"
$(APP): $(GO_FILES)
@go build $? -o $@
restart: kill before $(APP)
@app & echo $$! > $(PID)

.PHONY: serve restart kill before # let's go to reserve rules names
$ brew install fswatch # if you don't have
$ make serve

--

--

Software developer — https://github.com/olebedev

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store