gotips

gotips

Go语言编程技巧与最佳实践大全

gotips项目汇集了实用的Go编程技巧和最佳实践,涵盖性能优化、错误处理、代码风格和并发编程等多个方面。由众多贡献者维护更新,内容丰富全面,适合不同水平的Go开发者学习参考。项目旨在帮助程序员编写更高效可靠的Go代码,是提升Go编程能力的重要资源。

Go语言编程技巧开源项目翻译GitHubGithub

Go语言编程技巧

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

All Contributors

<!-- ALL-CONTRIBUTORS-BADGE:END -->

来自Phuong Le的Go技巧。

翻译后的网站: Go语言编程技巧 (自动生成)

微信讨论群:

<img src="https://yellow-cdn.veclightyear.com/835a84d5/ae111320-944a-45e2-8580-ee2febd5da66.png" width="200px">

翻译规范

  1. 创建一个认领issue,在标题中注明tip编号和标题。每人同一时间只能认领一个任务。完成一项任务后才能认领下一个。
  2. 请在Phuong Le的Twitter账号中找到对应的tip,然后将其翻译成中文。(注意:有些tip可能已被作者删除。经作者确认,内容会有增删,因此如果某个tip不存在,请跳过并寻找下一个tip)
  3. fork本项目,在fork后的项目中翻译你认领的tip,完成后提交pull request。
  4. 任务需在一周内完成,未完成的任务将被释放,其他人可以认领。
  • 项目会自动统计贡献者
  • 翻译时请保持语句通顺,可以使用AI辅助翻译,但务必避免生硬和机械翻译的感觉

如何提交Pull Request?

如果你以前没有提交过Pull Request,可以尝试以下步骤: 首先点击项目右上角的Fork按钮,将项目Fork到你自己的仓库。 在GitHub上浏览你fork的项目,你会看到一个"Create pull request"按钮,点击它。 填写相关内容并提交。 之后等待项目维护者合并你的PR即可。

翻译模板

# Tip #76 函数调用的结果回传 > 原始链接:[Golang Tip #76: Result forwarding in function call](https://twitter.com/func25/status/1779128931586850890) > 当我刚开始使用Go语言时,我发现有一个概念比较棘手:函数调用结果的回传。 ....
  • 在src目录下创建翻译文件: xxx.md,内容模板如上
  • 将图片放入 src/images 文件夹中
  • src/SUMMARY.md 文件中添加你翻译的项目
  • README 文件的翻译任务认领表格中填写你翻译的项目

翻译任务认领表格

tip序号标题译者
1只用一行代码测量函数执行时间smallnest
2多阶段延迟执行smallnest
3预分配切片以提高性能smallnest
4将数组解析为切片smallnest
5方法链式调用smallnest
6下划线导入icyfire
7作者已删除
8错误包装icyfire
9编译时接口验证icyfire
10避免使用裸参数smallnest
11数字分隔符icyfire
12避免使用math/rand,改用crypto/rand生成密钥icyfire
13空切片或更好的NIL切片icyfire
14错误消息不应大写或以标点符号结尾icyfire
15何时使用点(.)导入和空白(_)导入?qcrao
16不要使用-1或nil来表示错误kagaya85
17理解"快速返回,提前返回"以避免嵌套代码icyfire
18在消费者包中定义接口,而不是生产者包中syjs10
19除非必要用于文档说明,否则避免使用命名结果smallnest
20传递值,而不是指针smallnest
21定义方法时优先使用指针接收器QingyaFan
22使用结构体或可变参数选项简化函数签名zhubiaook
23为getter省略'Get'前缀HBUzxl
24避免命名重复smallnest
25在goroutine间通信时,优先使用'chan struct{}'而不是'chan bool'justlorain
26使用空白标识符(_)显式忽略值,而不是默默忽略smallnest
27无内存分配的过滤devin7788
28将多个if-else语句转换为switch语句zzzpppy
29避免使用context.Background(),使你的goroutine可承诺stonemax
30使用context.WithoutCancel()保持上下文持续smallnest
31使用循环标签实现更清晰的break和continuezhubiaook
32使用context.AfterFunc在上下文取消后调度函数LinPr,smallnest
33就是...不要Panic()baxiang
34以上下文开头,以选项结尾,始终以错误结束lylex
35在转换字符串时优先使用strconv而不是fmtjjjjjim
36使用下划线(_)前缀命名未导出的全局变量baxiang
37使用未导出的空结构体作为上下文键baxiang
38使用fmt.Errorf使你的错误更清晰,不要只留下裸错误smallnest
39避免在循环中使用defer,否则可能导致内存爆炸devin7788
40使用defer时处理错误,防止静默失败smallnest
41将结构体中的字段从大到小排序justlorain
42单次触碰错误处理,减少噪音zhubiaook
43优雅地关闭你的应用程序LinPr,smallnest
44使用Must函数有意地停止syjs10
45始终管理你的goroutine生命周期stonemax
46避免在switch语句中使用break,除非与标签配对使用baxiang
47表驱动测试、子测试和并行测试devin7788
48避免使用全局变量,尤其是可变的全局变量vcheckzen
49给调用者做决定的权利vcheckzen
50使结构体不可比较smallnest
51避免使用init()richzw
52在容器化环境(Kubernetes、Docker等)中调整GOMAXPROCSbaxiang
53枚举从1开始用于分类,0用于默认情况baxiang
54仅在客户端确实需要时才定义错误(var Err = errors.New)lylex
55通过使用空字段防止结构体未键入字面量cannian1
56简化接口,只询问你真正需要的内容cannian1
57Golang提示#57:Go中的标志枚举baxiang
58将互斥锁放在靠近它所保护的数据旁边richzw
59如果不需要参数,要么删除它,要么有意忽略它TravisRoad
60sync.Once是执行一次性操作的最佳方式smallnest
61创建具有内置锁定的类型(嵌入sync.Mutex)richzw
62context.Value不是我们的朋友hawkinggg
63避免使用time.Sleep(),它不支持上下文感知且无法中断richzw
64保持main()函数简洁可测试syjs10
65使用泛型简化返回指针miniLCT
66在fmt.Errorf中简化错误消息smallnest
67如何处理长函数签名richzw
68使用deadcode工具查找并删除未使用的函数richzw
69使用errgroup管理多个goroutinerichzw
70实现一个支持上下文的sleep函数hxzhouh
71sync.Pool,使用泛型实现类型安全QingyaFan
72使用strings.EqualFold进行不区分大小写的字符串比较syjs10
73使用stringer工具为枚举实现String()方法syjs10
74使time.Duration清晰易懂richzw
75使用singleflight优化多次调用hxzhouh
76函数调用中的结果转发syjs10
77使用带缓冲的通道作为信号量限制goroutine执行QingyaFan
78非阻塞通道发送技巧hxzhouh
79如果做了不寻常的事,请注释原因smallnest
80在使用变量附近声明变量smallnest
81检查字符串是否为空时,优先使用s != ""而不是len(s) == 0smallnest
82使用%+v和%q而不是%v和%s来增强调试输出smallnest

生成文档

如果你想在本地编译,请安装mdbook工具。

本地预览

在本地运行mdbook serve可以生成本地网站访问。

http://localhost:3000

生成静态网站

mdbook build 生成网站到book目录下。

贡献者 ✨

<!-- 所有贡献者列表:开始 - 请勿删除或修改此部分 --> <!-- prettier-ignore-start --> <!-- markdownlint-disable --> <table> <tbody> <tr> <td align="center" valign="top" width="14.28%"><a href="https://colobu.com/"><img src="https://avatars.githubusercontent.com/u/865763?v=4?s=100" width="100px;" alt="smallnest"/><br /><sub><b>smallnest</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=smallnest" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/hxzhouh"><img src="https://avatars.githubusercontent.com/u/25883521?v=4?s=100" width="100px;" alt="hxzhouh"/><br /><sub><b>hxzhouh</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=hxzhouh" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/icyfire"><img src="https://avatars.githubusercontent.com/u/1171180?v=4?s=100" width="100px;" alt="icyfire"/><br /><sub><b>icyfire</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=icyfire" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/syjs10"><img src="https://avatars.githubusercontent.com/u/15065304?v=4?s=100" width="100px;" alt="JS"/><br /><sub><b>JS</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=syjs10" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="http://stackoverflow.com/users/3011380/zangw"><img src="https://avatars.githubusercontent.com/u/1590890?v=4?s=100" width="100px;" alt="richzw"/><br /><sub><b>richzw</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=richzw" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/QingyaFan"><img src="https://avatars.githubusercontent.com/u/10420579?v=4?s=100" width="100px;" alt="cheerfun"/><br /><sub><b>cheerfun</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=QingyaFan" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/miniLCT"><img src="https://avatars.githubusercontent.com/u/45364609?v=4?s=100" width="100px;" alt="rkmdsxmds"/><br /><sub><b>rkmdsxmds</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=miniLCT" title="代码">💻</a></td> </tr> <tr> <td align="center" valign="top" width="14.28%"><a href="https://github.com/devin7788"><img src="https://avatars.githubusercontent.com/u/39721466?v=4?s=100" width="100px;" alt="devin7788"/><br /><sub><b>devin7788</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=devin7788" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/cannian1"><img src="https://avatars.githubusercontent.com/u/59365095?v=4?s=100" width="100px;" alt="Cannian"/><br /><sub><b>Cannian</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=cannian1" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://blog.lxythan2lxy.cn"><img src="https://avatars.githubusercontent.com/u/46567145?v=4?s=100" width="100px;" alt="卢修元"/><br /><sub><b>卢修元</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=TravisRoad" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/zzzpppy"><img src="https://avatars.githubusercontent.com/u/49774236?v=4?s=100" width="100px;" alt="zzzpppy"/><br /><sub><b>zzzpppy</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=zzzpppy" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="http://blog.kagaya.fun"><img src="https://avatars.githubusercontent.com/u/28755005?v=4?s=100" width="100px;" alt="Kagaya"/><br /><sub><b>Kagaya</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=kagaya85" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/stonemax"><img src="https://avatars.githubusercontent.com/u/4516172?v=4?s=100" width="100px;" alt="张金龙"/><br /><sub><b>张金龙</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=stonemax" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/HBUzxl"><img src="https://avatars.githubusercontent.com/u/86550622?v=4?s=100" width="100px;" alt="HBUzxl"/><br /><sub><b>HBUzxl</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=HBUzxl" title="代码">💻</a></td> </tr> <tr> <td align="center" valign="top" width="14.28%"><a href="http://yuque.com/baxiang"><img src="https://avatars.githubusercontent.com/u/2994323?v=4?s=100" width="100px;" alt="羊羽"/><br /><sub><b>羊羽</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=baxiang" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/zhubiaook"><img src="https://avatars.githubusercontent.com/u/30363122?v=4?s=100" width="100px;" alt="zhubiaook"/><br /><sub><b>zhubiaook</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=zhubiaook" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://logi.im"><img src="https://avatars.githubusercontent.com/u/18008498?v=4?s=100" width="100px;" alt="LOGI"/><br /><sub><b>LOGI</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=vcheckzen" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/justlorain"><img src="https://avatars.githubusercontent.com/u/87760338?v=4?s=100" width="100px;" alt="Lorain"/><br /><sub><b>Lorain</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=justlorain" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/lylex"><img src="https://avatars.githubusercontent.com/u/4638962?v=4?s=100" width="100px;" alt="lylex"/><br /><sub><b>lylex</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=lylex" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/LinPr"><img src="https://avatars.githubusercontent.com/u/56944601?v=4?s=100" width="100px;" alt="Lin"/><br /><sub><b>Lin</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=LinPr" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="http://qcrao.com"><img src="https://avatars.githubusercontent.com/u/7698088?v=4?s=100" width="100px;" alt="qcrao"/><br /><sub><b>qcrao</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=qcrao" title="代码">💻</a></td> </tr> <tr> <td align="center" valign="top" width="14.28%"><a href="https://ryan961.github.io"><img src="https://avatars.githubusercontent.com/u/32825698?v=4?s=100" width="100px;" alt="ryan961"/><br /><sub><b>ryan961</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=ryan961" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/ekkoful"><img src="https://avatars.githubusercontent.com/u/42363586?v=4?s=100" width="100px;" alt="Ekko"/><br /><sub><b>Ekko</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=ekkoful" title="代码">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/wzlove"><img src="https://avatars.githubusercontent.com/u/35476126?v=4?s=100" width="100px;" alt="whoops"/><br /><sub><b>whoops</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=wzlove" title="代码">💻</a></td> </tr> </tbody> </table> <!-- markdownlint-restore --> <!-- prettier-ignore-end --> <!-- 所有贡献者列表结束 -->

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

openai-agents-python

openai-agents-python

OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。

openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。

下拉加载更多