Go语言编程技巧
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

<!-- ALL-CONTRIBUTORS-BADGE:END -->
来自Phuong Le的Go技巧。
翻译后的网站: Go语言编程技巧 (自动生成)
微信讨论群:
<img src="https://yellow-cdn.veclightyear.com/835a84d5/ae111320-944a-45e2-8580-ee2febd5da66.png" width="200px">
翻译规范
- 创建一个认领issue,在标题中注明tip编号和标题。每人同一时间只能认领一个任务。完成一项任务后才能认领下一个。
- 请在Phuong Le的Twitter账号中找到对应的tip,然后将其翻译成中文。(注意:有些tip可能已被作者删除。经作者确认,内容会有增删,因此如果某个tip不存在,请跳过并寻找下一个tip)
- fork本项目,在fork后的项目中翻译你认领的tip,完成后提交pull request。
- 任务需在一周内完成,未完成的任务将被释放,其他人可以认领。
- 项目会自动统计贡献者
- 翻译时请保持语句通顺,可以使用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和continue | zhubiaook |
32 | 使用context.AfterFunc在上下文取消后调度函数 | LinPr,smallnest |
33 | 就是...不要Panic() | baxiang |
34 | 以上下文开头,以选项结尾,始终以错误结束 | lylex |
35 | 在转换字符串时优先使用strconv而不是fmt | jjjjjim |
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等)中调整GOMAXPROCS | baxiang |
53 | 枚举从1开始用于分类,0用于默认情况 | baxiang |
54 | 仅在客户端确实需要时才定义错误(var Err = errors.New) | lylex |
55 | 通过使用空字段防止结构体未键入字面量 | cannian1 |
56 | 简化接口,只询问你真正需要的内容 | cannian1 |
57 | Golang提示#57:Go中的标志枚举 | baxiang |
58 | 将互斥锁放在靠近它所保护的数据旁边 | richzw |
59 | 如果不需要参数,要么删除它,要么有意忽略它 | TravisRoad |
60 | sync.Once是执行一次性操作的最佳方式 | smallnest |
61 | 创建具有内置锁定的类型(嵌入sync.Mutex) | richzw |
62 | context.Value不是我们的朋友 | hawkinggg |
63 | 避免使用time.Sleep(),它不支持上下文感知且无法中断 | richzw |
64 | 保持main()函数简洁可测试 | syjs10 |
65 | 使用泛型简化返回指针 | miniLCT |
66 | 在fmt.Errorf中简化错误 消息 | smallnest |
67 | 如何处理长函数签名 | richzw |
68 | 使用deadcode工具查找并删除未使用的函数 | richzw |
69 | 使用errgroup管理多个goroutine | richzw |
70 | 实现一个支持上下文的sleep函数 | hxzhouh |
71 | sync.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) == 0 | smallnest |
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 -->
<!-- 所有贡献者列表结束 -->