Skip to content

Instantly share code, notes, and snippets.

@spalt08
Last active September 4, 2019 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spalt08/6ef0e6208246465841dddc16af3a7de3 to your computer and use it in GitHub Desktop.
Save spalt08/6ef0e6208246465841dddc16af3a7de3 to your computer and use it in GitHub Desktop.
Go live-reload snippet
# Variable for filename for store running procees id
PID_FILE = /tmp/my-app.pid
# We can use such syntax to get main.go and other root Go files.
GO_FILES = $(wildcard *.go)
# Start task performs "go run main.go" command and writes it's process id to PID_FILE.
start:
go run $(GO_FILES) & echo $$! > $(PID_FILE)
# You can also use go build command for start task
# start:
# go build -o /bin/my-app . && \
# /bin/my-app & echo $$! > $(PID_FILE)
# Stop task will kill process by ID stored in PID_FILE (and all child processes by pstree).
stop:
-kill `pstree -p \`cat $(PID)\` | tr "\n" " " |sed "s/[^0-9]/ /g" |sed "s/\s\s*/ /g"`
# Before task will only prints message. Actually, it is not necessary. You can remove it, if you want.
before:
@echo "STOPED my-app" && printf '%*s\n' "40" '' | tr ' ' -
# Restart task will execute stop, before and start tasks in strict order and prints message.
restart: stop before start
@echo "STARTED my-app" && printf '%*s\n' "40" '' | tr ' ' -
# Serve task will run fswatch monitor and performs restart task if any source file changed. Before serving it will execute start task.
serve: start
fswatch -or --event=Updated /home/parla-go/src | \
xargs -n1 -I {} make restart
# .PHONY is used for reserving tasks words
.PHONY: start before stop restart serve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment