golang构建工具Makefile使用详解
知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《golang构建工具Makefile使用详解》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!
正文
可能是因为编译太简单了,golang 并没有一个官方的构建工具(类似于 java 的 maven 和 gradle之类的),但是除了编译,我们可能还需要下载依赖,运行测试,甚至像 easyjson,protobuf,thrift 这样的工具下载和代码生成,如果没有构建工具,这些工作就会非常麻烦
为了解决这个问题,之前写过一个 everything.sh
的脚本,把所有的操作都封装在这个脚本里面,只需要执行类似于 sh everything.sh dependency
的命令就可以完成对应的工作,大大简化了构建过程,但是也有一个问题,shell 脚本本身的可读性并不是很好,而且对于各个操作之间的依赖不好描述
一次偶然的机会,在 github 上看到有人用 Makefile,就尝试了一下,发现真的非常合适,Makefile 本身就是用来描述依赖的,可读性非常好,而且与强大的 shell 结合在一起,基本可以实现任何想要的功能
下面是我在实际项目中使用的一个 Makefile,支持的功能包括
make build
: 编译
make vendor
: 下载依赖
make api
: 生成协议代码
make json
: easyjson 代码生成
make test
: 运行单元测试
make benchmark
: 运行性能测试
make stat
: 代码复杂度统计,代码行数统计
make clean
: 清理 build 目录
make deep_clean
: 清理所有代码以外的其他文件
make third
: 下载所有依赖的第三方工具
make protoc
: 下载 protobuf 工具
make glide
: 下载 glide 依赖管理工具
make golang
: 下载 golang 环境
make cloc
: 下载 cloc 统计工具
make gocyclo
: 下载 gocyclo 圈复杂度计算工具
make easyjson
: 下载 easyjson 工具
export PATH:=${PATH}:${GOPATH}/bin:$(shell pwd)/third/go/bin:$(shell pwd)/third/protobuf/bin:$(shell pwd)/third/cloc-1.76 .PHONY: all all: third vendor api json build test stat build: cmd/rta_server/*.go internal/*/*.go scripts/version.sh Makefile vendor api json @echo "编译" @rm -rf build/ && mkdir -p build/bin/ && \ go build -ldflags "-X 'main.AppVersion=`sh scripts/version.sh`'" cmd/rta_server/main.go && \ mv main build/bin/rta_server && \ cp -r configs build/configs/ vendor: glide.lock glide.yaml @echo "下载 golang 依赖" glide install api: vendor @echo "生成协议文件" @rm -rf api && mkdir api && \ cd vendor/gitlab.mobvista.com/vta/rta_proto.git/ && \ protoc --go_out=plugins=grpc:. *.proto && \ cd - && \ cp vendor/gitlab.mobvista.com/vta/rta_proto.git/* api/ json: internal/rcommon/rta_common_easyjson.go internal/rcommon/rta_common_easyjson.go: internal/rcommon/rta_common.go Makefile easyjson internal/rcommon/rta_common.go .PHONY: test test: vendor api json @echo "运行单元测试" go test -cover internal/rranker/*.go go test -cover internal/rserver/*.go go test -cover internal/rworker/*.go go test -cover internal/rloader/*.go go test -cover internal/rrecall/*.go go test -cover internal/rmaster/*.go go test -cover internal/rsender/*.go benchmark: benchmarkloader benchmarkall .PHONY: benchmarkloader benchmarkloader: vendor api json @echo "运行 loader 性能测试" go test -timeout 2h -bench BenchmarkS3Loader_Load -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rloader/* go tool pprof -svg ./rloader.test cpu.out > cpu.benchmarkloader.svg go tool pprof -svg ./rloader.test mem.out > mem.benchmarkloader.svg .PHONY: benchmarkserver benchmarkserver: vendor api json @echo "运行 server 性能测试" go test -timeout 2h -bench BenchmarkServer -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rserver/* go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkserver.svg go tool pprof -svg ./rserver.test mem.out > mem.benchmarkserver.svg .PHONY: benchmarkall benchmarkall: vendor api json @echo "运行 server 性能测试" go test -timeout 2h -bench BenchmarkAll -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rserver/* go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkall.svg go tool pprof -svg ./rserver.test mem.out > mem.benchmarkall.svg .PHONY: benchmarkcache benchmarkcache: vendor api json @echo "测试 redis 集群性能" go test -timeout 5m -bench BenchmarkRtaCacheBatch -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rserver/* .PHONY: stat stat: cloc gocyclo @echo "代码行数统计" @ls internal/*/* scripts/* configs/* Makefile | xargs cloc --by-file @echo "圈复杂度统计" @ls internal/*/* | grep -v _test | xargs gocyclo @ls internal/*/* | grep -v _test | xargs gocyclo | awk '{sum+=?1}END{printf("总圈复杂度: %s", sum)}' .PHONY: clean clean: rm -rf build .PHONY: deep_clean deep_clean: rm -rf vendor api build third third: protoc glide golang cloc gocyclo easyjson .PHONY: protoc protoc: golang @hash protoc 2>/dev/null || { \ echo "安装 protobuf 代码生成工具 protoc" && \ mkdir -p third && cd third && \ wget https://github.com/google/protobuf/releases/download/v3.2.0/protobuf-cpp-3.2.0.tar.gz && \ tar -xzvf protobuf-cpp-3.2.0.tar.gz && \ cd protobuf-3.2.0 && \ ./configure --prefix=`pwd`/../protobuf && \ make -j8 && \ make install && \ cd ../.. && \ protoc --version; \ } @hash protoc-gen-go 2>/dev/null || { \ echo "安装 protobuf golang 插件 protoc-gen-go" && \ go get -u github.com/golang/protobuf/{proto,protoc-gen-go}; \ } .PHONY: glide glide: golang @mkdir -p ?GOPATH/bin @hash glide 2>/dev/null || { \ echo "安装依赖管理工具 glide" && \ curl https://glide.sh/get | sh; \ } .PHONY: golang golang: @hash go 2>/dev/null || { \ echo "安装 golang 环境 go1.10" && \ mkdir -p third && cd third && \ wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz && \ tar -xzvf go1.10.linux-amd64.tar.gz && \ cd .. && \ go version; \ } .PHONY: cloc cloc: @hash cloc 2>/dev/null || { \ echo "安装代码统计工具 cloc" && \ mkdir -p third && cd third && \ wget https://github.com/AlDanial/cloc/archive/v1.76.zip && \ unzip v1.76.zip; \ } .PHONY: gocyclo gocyclo: golang @hash gocyclo 2>/dev/null || { \ echo "安装代码圈复杂度统计工具 gocyclo" && \ go get -u github.com/fzipp/gocyclo; \ } .PHONY: easyjson easyjson: golang @hash easyjson 2>/dev/null || { \ echo "安装 json 编译工具 easyjson" && \ go get -u github.com/mailru/easyjson/...; \ }
今天关于《golang构建工具Makefile使用详解》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

- 上一篇
- go单体日志采集zincsearch方案实现

- 下一篇
- Makefile构建Golang项目示例详解
-
- 洁净的苗条
- 这篇技术贴真及时,太细致了,感谢大佬分享,码起来,关注作者大大了!希望作者大大能多写Golang相关的文章。
- 2023-01-29 11:12:40
-
- 忧郁的太阳
- 这篇博文真及时,细节满满,很棒,码起来,关注师傅了!希望师傅能多写Golang相关的文章。
- 2023-01-12 20:01:37
-
- 轻松的月饼
- 太全面了,mark,感谢老哥的这篇技术贴,我会继续支持!
- 2023-01-10 09:10:24
-
- 超帅的小松鼠
- 很棒,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢楼主分享技术文章!
- 2023-01-09 13:06:18
-
- Golang · Go教程 | 1小时前 |
- DebianHadoop性能瓶颈分析与优化方案
- 382浏览 收藏
-
- Golang · Go教程 | 2小时前 |
- DebianSyslog日志备份与恢复攻略
- 370浏览 收藏
-
- Golang · Go教程 | 3小时前 |
- DebianFTPServer兼容性问题终极解决方案
- 102浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- DebianStrings源码深度解析
- 148浏览 收藏
-
- Golang · Go教程 | 7小时前 |
- FetchDebian下载问题解决攻略
- 350浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 笔灵AI生成答辩PPT
- 探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
- 20次使用
-
- 知网AIGC检测服务系统
- 知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
- 29次使用
-
- AIGC检测-Aibiye
- AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
- 35次使用
-
- 易笔AI论文
- 易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
- 43次使用
-
- 笔启AI论文写作平台
- 笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
- 36次使用
-
- Makefile构建Golang项目示例详解
- 2023-01-21 185浏览
-
- Go项目编写Makefile规则文件概述
- 2022-12-23 147浏览