Skip to content

Instantly share code, notes, and snippets.

@tinti
Created March 12, 2019 15:38
Show Gist options
  • Save tinti/65a1b0997d3506d4daed5f1aca0f15ac to your computer and use it in GitHub Desktop.
Save tinti/65a1b0997d3506d4daed5f1aca0f15ac to your computer and use it in GitHub Desktop.
Makefile for Go Projects
GOPATH=$(shell pwd)/vendor:$(shell pwd)
GOBIN=$(shell pwd)/bin
GOFILES=$(wildcard *.go)
GONAME=$(shell basename "$(PWD)")
PID=/tmp/go-$(GONAME).pid
build:
@echo "Building $(GOFILES) to ./bin"
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build -o bin/$(GONAME) $(GOFILES)
get:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get .
install:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install $(GOFILES)
run:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go run $(GOFILES)
watch:
@$(MAKE) restart &
@fswatch -o . -e 'bin/.*' | xargs -n1 -I{} make restart
restart: clear stop clean build start
start:
@echo "Starting bin/$(GONAME)"
@./bin/$(GONAME) & echo $$! > $(PID)
stop:
@echo "Stopping bin/$(GONAME) if it's running"
@-kill `[[ -f $(PID) ]] && cat $(PID)` 2>/dev/null || true
clear:
@clear
clean:
@echo "Cleaning"
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean
.PHONY: build get install run watch start stop restart clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment