Last active
February 28, 2025 06:07
-
Star
(172)
You must be signed in to star a gist -
Fork
(52)
You must be signed in to fork a gist
-
-
Save alexedwards/3b40775846535d0014ab1ff477e4a568 to your computer and use it in GitHub Desktop.
Boilerplate Makefile for Go projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Change these variables as necessary. | |
main_package_path = ./cmd/example | |
binary_name = example | |
# ==================================================================================== # | |
# HELPERS | |
# ==================================================================================== # | |
## help: print this help message | |
.PHONY: help | |
help: | |
@echo 'Usage:' | |
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' | |
.PHONY: confirm | |
confirm: | |
@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ] | |
.PHONY: no-dirty | |
no-dirty: | |
@test -z "$(shell git status --porcelain)" | |
# ==================================================================================== # | |
# QUALITY CONTROL | |
# ==================================================================================== # | |
## audit: run quality control checks | |
.PHONY: audit | |
audit: test | |
go mod tidy -diff | |
go mod verify | |
test -z "$(shell gofmt -l .)" | |
go vet ./... | |
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./... | |
go run golang.org/x/vuln/cmd/govulncheck@latest ./... | |
## test: run all tests | |
.PHONY: test | |
test: | |
go test -v -race -buildvcs ./... | |
## test/cover: run all tests and display coverage | |
.PHONY: test/cover | |
test/cover: | |
go test -v -race -buildvcs -coverprofile=/tmp/coverage.out ./... | |
go tool cover -html=/tmp/coverage.out | |
# ==================================================================================== # | |
# DEVELOPMENT | |
# ==================================================================================== # | |
## tidy: tidy modfiles and format .go files | |
.PHONY: tidy | |
tidy: | |
go mod tidy -v | |
go fmt ./... | |
## build: build the application | |
.PHONY: build | |
build: | |
# Include additional build steps, like TypeScript, SCSS or Tailwind compilation here... | |
go build -o=/tmp/bin/${binary_name} ${main_package_path} | |
## run: run the application | |
.PHONY: run | |
run: build | |
/tmp/bin/${binary_name} | |
## run/live: run the application with reloading on file changes | |
.PHONY: run/live | |
run/live: | |
go run github.com/cosmtrek/[email protected] \ | |
--build.cmd "make build" --build.bin "/tmp/bin/${binary_name}" --build.delay "100" \ | |
--build.exclude_dir "" \ | |
--build.include_ext "go, tpl, tmpl, html, css, scss, js, ts, sql, jpeg, jpg, gif, png, bmp, svg, webp, ico" \ | |
--misc.clean_on_exit "true" | |
# ==================================================================================== # | |
# OPERATIONS | |
# ==================================================================================== # | |
## push: push changes to the remote Git repository | |
.PHONY: push | |
push: confirm audit no-dirty | |
git push | |
## production/deploy: deploy the application to production | |
.PHONY: production/deploy | |
production/deploy: confirm audit no-dirty | |
GOOS=linux GOARCH=amd64 go build -ldflags='-s' -o=/tmp/bin/linux_amd64/${binary_name} ${main_package_path} | |
upx -5 /tmp/bin/linux_amd64/${binary_name} | |
# Include additional deployment steps here... |
@megasuperlexa why not? make
is almost ubiquitous, it's a good starting point IMHO!
Nice little project, may I suggest to add a license to this?
@graugans Consider it published under the MIT license.
so useful
very useful!
@alexedwards I didn't know uxp
and discovered it here.
Very nice! Thanks for sharing 🚀
Nice. Is it public domain so I can just use it in my projects? Or under any special license?
@janderssonse Please see the comment 4 above yours 👍
Excellent!
I made a small change on setting binary_name
:
binary_name = $(awk '/^module/{print $2}' go.mod)
Extract binary_name
from go.mod
, module
line.
Lots of good ideas here and food for thought. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so all targets are phony, what is the point of using
make
in this case? maybe just use plain bash?