DEV Community

DevByJ
DevByJ

Posted on

2 1

A basic Golang project Makefile

In some of my side projects, I develop with Golang. The CLI tool Make is mainly used there 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)

Enter fullscreen mode Exit fullscreen mode

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. These are very powerful, because in addition to code execution and build, there are Go tools for analysis as well as testing and testing with coverage view in the browser included. In addition, there is the management of to-do notes and the deletion of the coverage and build directories.

Top comments (0)

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay