golang makefile使用

makefile运行go常用命令

go经常会执行:测试、编译、运行、语法检查等命令

go vet 静态检查
go test 运行单元测试
go fmt 格式化
go build 编译
go run 运行 ...

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 clean

makefile示例二

.PHONY: all build clean run check cover lint docker help
BIN_FILE=hello
all: check build
build:
    @go build -o "${BIN_FILE}"
clean:
    @go clean
    rm --force "xx.out"
test:
    @go test
check:
    @go fmt ./
    @go vet ./
cover:
    @go test -coverprofile xx.out
    @go tool cover -html=xx.out
run:
    ./"${BIN_FILE}"
lint:
    golangci-lint run --enable-all
docker:
    @docker build -t leo/hello:latest .
help:
    @echo "make 格式化go代码 并编译生成二进制文件"
    @echo "make build 编译go代码生成二进制文件"
    @echo "make clean 清理中间目标文件"
    @echo "make test 执行测试case"
    @echo "make check 格式化go代码"
    @echo "make cover 检查测试覆盖率"
    @echo "make run 直接运行程序"
    @echo "make lint 执行代码检查"
    @echo "make docker 构建docker镜像"

相关链接

https://juejin.cn/post/6844903806971412494
https://studygolang.com/articles/14919
https://zhuanlan.zhihu.com/p/345342203
https://github.com/thockin/go-build-template
https://gist.github.com/TheHippo/7e4d9ec4b7ed4c0d7a39839e6800cc16
https://github.com/azer/go-makefile-example/blob/master/Makefile

【励志篇】: 古之成大事掌大学问者,不惟有超世之才,亦必有坚韧不拔之志。
原文地址:https://www.cnblogs.com/tomtellyou/p/14836995.html