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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM