Mastering Makefiles for Go Projects: A Complete Backend Development Guide
This guide explains how to create effective Makefiles for Go projects, covering prerequisites, syntax, common Go commands, a full example Makefile, and integration with CI/CD pipelines to streamline building, testing, and deployment.
1. Prerequisites
Familiar with Makefile
Familiar with Go projects
During development you often need to compile, run, test, format, and check code. While you can run go build, go run, etc., a Makefile can automate these tasks.
2. Makefile Syntax
PROJECT="example"
default:
echo ${PROJECT}
install:
@govendor sync -v
test: install
@go test ./...
.PHONY: default install testRunning make, make install, or make test executes the corresponding commands.
The basic format is:
<target> : <prerequisites>
<tab><commands>target – the custom command name
prerequisites – commands run before the target
commands – the actual shell commands
.PHONY – declares phony targets
First target is the default
@ suppresses command echo
# starts a comment
${val} references a variable
Wildcards are allowed
3. Go Project Commands
Typical Go commands include:
go vet – static analysis
go test – unit testing
go fmt – formatting
go build – compilation
go run – execution
A Makefile for a Go project should provide targets such as:
make default – build
make fmt – format
make vet – static check
make test – run tests
make install – download dependencies
make clean – remove binaries
Example full Makefile:
BINARY="example"
VERSION=1.0.0
BUILD=`date +%FT%T%z`
PACKAGES=`go list ./... | grep -v /vendor/`
VETPACKAGES=`go list ./... | grep -v /vendor/ | grep -v /examples/`
GOFILES=`find . -name "*.go" -type f -not -path "./vendor/*"`
default:
@go build -o ${BINARY} -tags=jsoniter
list:
@echo ${PACKAGES}
@echo ${VETPACKAGES}
@echo ${GOFILES}
fmt:
@gofmt -s -w ${GOFILES}
fmt-check:
@diff=$(gofmt -s -d $(GOFILES)); \
if [ -n "$diff" ]; then \
echo "Please run 'make fmt' and commit the result:"; \
echo "$diff"; \
exit 1; \
fi
install:
@govendor sync -v
test:
@go test -cpu=1,2,4 -v -tags integration ./...
vet:
@go vet $(VETPACKAGES)
docker:
@docker build -t wuxiaoxiaoshen/example:latest .
clean:
@if [ -f ${BINARY} ]; then rm ${BINARY}; fi
.PHONY: default fmt fmt-check install test vet docker clean4. Additional Notes
Makefiles simplify build automation and are often integrated with CI/CD pipelines, such as Travis CI, to run tests, static checks, and build Docker images automatically.
Source: https://juejin.cn/post/6844903806971412494
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
