A basic Golang project Makefile

In some of my side projects, I develop with Golang. The Make tool is mainly used to execute build and test commands, but it is also used for other tasks. After some time, the following Makefile has become established with me, which I would like to share. .PHONY: todo analyse test coverage run build clean COVERAGE_DIR := ./coverage BUILD_DIR := ./build todo: @egrep --recursive --include "*" --exclude "Makefile" "TODO:" . || true analyse: @go vet ./... test: @go test -cover ./... coverage: @mkdir -p $(COVERAGE_DIR) @go test -covermode=count -coverprofile=$(COVERAGE_DIR)/cover.out ./... @go tool cover -html=$(COVERAGE_DIR)/cover.out -o $(COVERAGE_DIR)/cover.html @open $(COVERAGE_DIR)/cover.html run: @go run . build: @go build -o $(BUILD_DIR)/app clean: @rm -rf $(COVERAGE_DIR) $(BUILD_DIR) My goal was to store and execute all the commands I need for development in Make. The "target with prerequisites mechanism" is not used here. Basically, behind most targets are commands with Go tools. In addition, there is the management of to-do notes and the deletion of the coverage and build directories.

Apr 15, 2025 - 21:51
 0
A basic Golang project Makefile

In some of my side projects, I develop with Golang. The Make tool is mainly used to execute build and test commands, but it is also used for other tasks. After some time, the following Makefile has become established with me, which I would like to share.

.PHONY: todo analyse test coverage run build clean

COVERAGE_DIR := ./coverage
BUILD_DIR    := ./build

todo:
    @egrep --recursive --include "*" --exclude "Makefile" "TODO:" . || true

analyse:
    @go vet ./...

test:
    @go test -cover ./...

coverage:
    @mkdir -p $(COVERAGE_DIR)
    @go test -covermode=count -coverprofile=$(COVERAGE_DIR)/cover.out ./...
    @go tool cover -html=$(COVERAGE_DIR)/cover.out -o $(COVERAGE_DIR)/cover.html
    @open $(COVERAGE_DIR)/cover.html

run:
    @go run .

build:
    @go build -o $(BUILD_DIR)/app

clean:
    @rm -rf $(COVERAGE_DIR) $(BUILD_DIR)

My goal was to store and execute all the commands I need for development in Make. The "target with prerequisites mechanism" is not used here. Basically, behind most targets are commands with Go tools. In addition, there is the management of to-do notes and the deletion of the coverage and build directories.