Live code reloading for Golang project

go Nov 29, 2017

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

Tags

Julien Maitrehenry

I specialize in DevOps, Agile practices and web development. I love sharing my knowledge for helping other people to go to the next level!

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.