Skip to content

Golang Makefile Standardization Tutorial: Best Practice Template for Automatically Generating Help ​

Golang Makefile Standardization Tutorial: Best Practice Template Diagram for Automatically Generating Help

In a Golang project,go build,go test,go vet These commands are typed almost every day, but few people seriously maintain a Makefile: targets are scattered, naming is arbitrary, and there are no comments; when newcomers take over, they can only guess.make Once executed, it may also accidentally trigger dangerous operations. The result is that automation did not save time; instead, it increased communication costs.

This tutorial is aimed at all Golang projects, from Directory structure, goal planning, and commenting standards Starting from, I’ll guide you step by step to write a well-structured, easy-to-maintain Makefile, and with the help of awk Implement make help Automatically generate help documentation——Adding a new target only requires adding one line.## 说明 Help information is automatically updated, so no manual maintenance is required.

A copy is provided in the text.Complete Makefile template that can be reused directly, coverage build,run,test,fmt,vet,clean,help and other common targets, along with extended recommendations such as team collaboration conventions, cross-compilation, and CI integration, suitable for Golang developers and operations teams to implement directly, improving project maintainability and collaboration efficiency.

An example of a standard Go project directory:

your-project/
├── Makefile
├── go.mod
└── cmd/
  └── <app-name>/
    └── main.go
  • Makefile: Project automation task entry

  • cmd/<app-name>/main.go: Main program entry

  • go.mod/go.sum: Go Modules dependency management

2. Makefile Target Planning ​

It is recommended to set the following common targets for a typical Golang project:

Target NameExplanation
buildCompile the project and generate the binary.
runRun the main program
testRun all tests
fmtCode formatting
vetStatic analysis
cleanClean build artifacts
helpAutomatically output help documentation

Tip

You can extend targets such as lint, docker, release, install, etc. according to actual needs; all targets must be accompanied by explanatory comments.

3. Makefile Comment Conventions and Automatic Help Mechanism ​

Add to each goal ## 描述 Annotation, cooperation awk Automatically generate help. Example:

makefile
build: ## Compile project
	...

run: ## Run the main program
	...

help Goal examples (automatically identify all goals with ## comments):

makefile
help: ## Display help information
	@echo "Usage: make [target]"
	@awk -F':.*## ' '/^[a-zA-Z0-9_-]+:.*## / && $$1 != "all" && $$1 != "help" {printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@awk -F':.*## ' '/^help:.*## / {printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

Like this, execute.make or make help, and all available commands and instructions will be automatically output.

4. Makefile variable setting recommendations ​

Standardize variable naming to facilitate maintenance and migration. For example:

makefile
PROJECT_DIR := .
APP_NAME := apiserver
PKG := $(PROJECT_DIR)/...
GO := go
  • PROJECT_DIR: Project main directory

  • APP_NAME: Application Name

  • PKG: Go package path,./... Indicates all subpackages recursively

5. Complete standardized Makefile template ​

Compatible with the vast majority of Golang projects, recommended to reuse directly:

makefile
PROJECT_DIR := .
APP_NAME := apiserver
PKG := $(PROJECT_DIR)/...
GO := go

.PHONY: all build clean test fmt vet run help

all: help

build: ## Compile project
	@mkdir -p $(PROJECT_DIR)/out
	$(GO) build -o $(PROJECT_DIR)/out/$(APP_NAME) $(PROJECT_DIR)/cmd/$(APP_NAME)

run: ## Run the main program
	$(GO) run $(PROJECT_DIR)/cmd/$(APP_NAME)

test: ## Run all tests
	$(GO) test -v $(PKG)

fmt: ## Code Formatting
	$(GO) fmt $(PKG)

vet: ## Static Analysis
	$(GO) vet $(PKG)

clean: ## Clean build artifacts
	rm -rf $(PROJECT_DIR)/out/

help: ## Display help information
	@echo "Usage: make [target]"
	@awk -F':.*## ' '/^[a-zA-Z0-9_-]+:.*## / && $$1 != "all" && $$1 != "help" {printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@awk -F':.*## ' '/^help:.*## / {printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

6. Recommendations for writing and maintenance ​

  • Target order and default behavior Usually all As the entry point, output help information by default to prevent accidental operations.

  • Automatic directory creation Add to the build target @mkdir -p, to prevent the output directory from being missing.

  • Just maintain the comments and help will be generated automatically. Adding a new target only requires supplementing ## 说明 help will be displayed automatically; no need to manually maintain help text.

  • Team Collaboration Guidelines It is recommended to standardize the Makefile style within the team to improve readability and development efficiency.

7. FAQs and Extensions ​

  • How to extend targets such as lint, docker, and release? Follow the existing target format to add new targets and supplement.## 说明 That is sufficient.

  • How to support multi-platform cross-compilation? Add environment variables in the build target, for example: GOOS=linux GOARCH=amd64 $(GO) build ...

  • How to integrate the CI toolchain? Simply invoke the Makefile target directly, e.g.make test,make build.

8. Summary ​

Through standardized Makefile design, Golang projects can achieve:

  • Clear division of tasks

  • Automated development process

  • Consistent and easy-to-use development experience

  • Automatically generated command help documentation

Just copy the template from this tutorial and make slight adjustments based on your project directory structure to quickly get a professional-grade Golang Makefile!

最后更新2026/11/07 14:16
如果你觉得这篇文章有帮助,或者想聊聊技术、工作,欢迎通过下面方式联系我:
contact fishfinal