golangmakefile使⽤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}
golang语法
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镜像"
相关链接

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。