Skip to content

Commit

Permalink
Merge pull request #161 from baxiang/master
Browse files Browse the repository at this point in the history
# 156 Naming Unexported Global Variables with an Underscore (_) Prefix
  • Loading branch information
smallnest authored Apr 26, 2024
2 parents 50fca65 + fa99ad3 commit a503278
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 33 | Just... Don’t Panic() | baxiang |
| 34 | Lead with context, end with options, and always close with an error | lylex |
| 35 | Prefer strconv over fmt for converting to/from string | jjjjjim |
| 36 | | |
| 36 | Naming Unexported Global Variables with an Underscore (_) Prefix | baxiang |
| 37 | Using Unexported Empty Struct as Context Key | baxiang |
| 38 | Make your errors clear with fmt.Errorf, don't just leave them bare | smallnest |
| 39 | Avoid defer in loops, or your memory might blow up | devin7788 |
Expand Down
32 changes: 32 additions & 0 deletions src/036.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Tip #36:以下划线(_)作为前缀,命名非导出的全局变量
> 原始链接:[Golang Tip #36: Naming Unexported Global Variables with an Underscore (_) Prefix.](https://twitter.com/func25/status/1763527891466522656)
>
(注:此命名规则在Go社区并没有被普遍认可为惯用做法,而是受到Uber编码风格指南的启发)

在Go语言中,声明在顶层的变量和常量可以在它们所属的整个包中被访问。

**常规命名方式有何不妥?**

如果没有明确的命名约定,我们很容易在更小的作用域内无意中覆盖这些包级别的变量。
![](./images/036/36_01.png)

设想一下,一个命名为 dataSize 的局部变量可能会覆盖同名的全局变量。

> 但如果它们的命名不同,还会有什么问题呢?”
![](./images/036/36_02.png)

尽管这个例子看似简单,但它却引发了一个问题:我们如何知道 maxUsers 变量的来源?

- 它是一个像 'limit' 这样的局部变量?
- 还是函数的一个参数?
- 或者是来自全局作用域?

在更复杂的场景中,我们可能不得不四处搜索或使用IDE的快捷键(如cmd + click)来查找并跳转到变量的定义。这个过程可能会分散我们的注意力并打断我们的工作流程。

**使用下划线前缀**

通过在全局变量前添加下划线 (_),可以明确表示这些标识符是全局的:
![](./images/036/36_03.png)

这种明确的标识使得“_maxUsers”被一眼识别为全局变量,大大降低了我们无意中覆盖或修改它的风险。
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [Tip #33 尽量...不要使用panic()](./033.md)
- [Tip #34 以context开头,以options结尾,并且总是用error来关闭](./034.md)
- [Tip #35 转换字符串时优先使用 strconv 而非 fmt](./035.md)
- [Tip #36 以下划线(_)作为前缀,命名非导出的全局变量](./036.md)
- [Tip #37 使用未导出的空结构体作为上下文键](./037.md)
- [Tip #38 使用 fmt.Errorf 使你的错误信息清晰明了,不要让它们过于赤裸](./038.md)
- [Tip #39 避免在循环中使用defer,否则可能会导致内存溢出](./039.md)
Expand Down
Binary file added src/images/036/36_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/036/36_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/036/36_03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a503278

Please sign in to comment.