Skip to content

Commit

Permalink
Merge pull request #129 from zhubiaook/master
Browse files Browse the repository at this point in the history
Fixes #125 translated tip#31
  • Loading branch information
smallnest authored Apr 24, 2024
2 parents 75da7cb + 9cc2a28 commit e6decb7
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 19 | | |
| 20 | Pass values, not pointers | smallnest |
| 21 | | |
| 22 | | |
| 22 | Simplify function signatures with structs or variadic options | zhubiaook |
| 23 | Skip the 'Get' prefix for getters | HBUzxl |
| 24 | | |
| 25 | Prefer 'chan struct{}' over 'chan bool' for signaling between goroutines | justlorain |
Expand All @@ -84,7 +84,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 28 | Converting multiple if-else statements into switch cases | zzzpppy |
| 29 | Avoid context.Background(), make your goroutines promisable. | stonemax |
| 30 | 使用context.WithoutCancel()继续上下文操作 | smallnest |
| 31 | | |
| 31 | Loop labels for cleaner breaks and continues | zhubiaook |
| 32 | | |
| 33 | | |
| 34 | | |
Expand Down
44 changes: 44 additions & 0 deletions src/031.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Tip #31 使用跳转标签让break和continue语句更简洁

> 原始链接:[Golang Tip #31: Loop labels for cleaner breaks and continues](https://twitter.com/func25/status/1761694371240702098)
>
通常避免使用标签和`goto` 语句,因为会降低代码可读性,使其难以理解。

![](./images/031/031_01.png)

上面简短的示例看起来很清晰易懂。但是随着代码复杂度的增加,可读性会大大降低:

* 你可能要在距离`goto` 语句“数百英里”之外才能找到目的标签。

* 你需要在代码上下文中去寻找`goto`语句跳转的目的标签在哪里。

### 跳转标签

例如,在处理嵌套循环时,某些情况下使用跳转标签是很不错的实践。

想象一下,我们在二维数组中搜索一个数字:

![](./images/031/031_02.png)

此时,你有一个更优雅的解决方案:在循环语句处声明跳转标签。

一旦声明了标签以后,你就可以使用`break``continue` 后跟一个标签实现不仅本层循环的跳转,任何外层循环的跳转都可以。

这样做的结果是?

代码不仅简短,而且更加清晰、明了。

![](./images/031/031_03.png)

我们可以在`break``continue` 语句中都使用标签。

另外一个有用的实例是当循环代码块中包含`select{}`

如果你在`select` 代码块中使用了不带标签的`break`,只会跳出`select`代码块,而不会跳出包含它的外层循环。

![](./images/031/031_04.png)

本节的技巧主要针对循环,但也可以使用在其他地方,比如`switch`实例:

![](./images/031/031_05.png)
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [Tip #28 将多个if-else语句转换为switch](./028.md)
- [Tip #29 避免使用 context.Background,使你的协程具备承诺性](./029.md)
- [Tip #30 使用context.WithoutCancel()继续上下文操作](./030.md)
- [Tip #31 使用跳转标签让break和continue语句更简洁](./031.md)
- [Tip #38 使用 fmt.Errorf 使你的错误信息清晰明了,不要让它们过于赤裸](./038.md)
- [Tip #40 在使用defer时处理错误以防止静默失败](./040.md)
- [Tip #43 优雅关闭你的应用程序](./043.md)
Expand Down
Binary file removed src/images/.DS_Store
Binary file not shown.
Binary file added src/images/031/031_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/031/031_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/031/031_03.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/031/031_04.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/031/031_05.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 e6decb7

Please sign in to comment.