Golang makefile example

Here is a simple example of a makefile for a golang project.

.DEFAULT_GOAL := build

BINARY="BinaryName"

fmt:
    go fmt ./
.PHONY: fmt

lint: fmt
    golint ./
.PHONY: lint

vet: fmt
    go vet ./
.PHONY: vet

build: vet
    go build hello.go
.PHONY:build

mac:
    CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ${BINARY}

win:
    CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ${BINARY}

run:
    @go run ./

gotool:
    go fmt ./
    go vet ./

clean:
    @if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

help:
    @echo "make - clean, format, build"
    @echo "make build - just build"
    @echo "make run - just run"
    @echo "make clean - clean binary file"
    @echo "make gotool - run fmt and vet tools"
    @echo "make mac - build for mac"
    @echo "make win - build for windows"

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback: dig +short txt issue.imzye.com
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Buy Me a Coffee