Skip to content

Commit

Permalink
finished
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Apr 27, 2024
1 parent 17b11d2 commit 1dd9aa9
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 33 deletions.
46 changes: 13 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).

<img src="src/images/wechat.png" width="200px">

**翻译进度:**74/78 = 95%
**翻译进度:**78/78 = 100%

## 翻译规范

Expand Down Expand Up @@ -59,7 +59,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 4 | Parse an Array into a Slice | smallnest |
| 5 | Method Chaining | smallnest |
| 6 | Underscore Import | icyfire |
| 7 | 作者已删除| |
| 7 | 作者已删除| |
| 8 | Wrapping Errors | icyfire |
| 9 | Compile-Time Interface Verification | icyfire |
| 10 | Avoid Naked Parameters | smallnest |
Expand All @@ -68,10 +68,10 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 13 | Empty slice or, even better, NIL SLICE | icyfire |
| 14 | Error messages should not be capitalized or end with punctuation | icyfire |
| 15 | When to use Dot (.) Import and Blank (_) Import? | qcrao |
| 16 | Don't Return -1 or nil to Indicate Error. | kagaya85 |
| 16 | Don't Return -1 or nil to Indicate Error. | kagaya85 |
| 17 | Understanding "Return fast, return early" to avoid nested code | icyfire |
| 18 | Define interfaces in the consumer package, not the producer | syjs10 |
| 19 | 已认领 | |
| 18 | Define interfaces in the consumer package, not the producer | syjs10 |
| 19 | Avoid named results unless necessary for documentation. | smallnest |
| 20 | Pass values, not pointers | smallnest |
| 21 | Prefer using a pointer receiver when defining methods | QingyaFan |
| 22 | Simplify function signatures with structs or variadic options | zhubiaook |
Expand All @@ -96,7 +96,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 41 | Sort your fields in your struct from largest to smallest | justlorain |
| 42 | Single Touch Error Handling, Less Noise. | zhubiaook |
| 43 | Gracefully Shut Down Your Application | LinPr,smallnest |
| 44 | Intentionally Stop with Must Functions | syjs10 |
| 44 | Intentionally Stop with Must Functions | syjs10 |
| 45 | Always Manage Your Goroutine Lifetime. | stonemax |
| 46 | Avoid using break in switch cases, except when paired with labels | baxiang |
| 47 | Table-driven tests, subtests, and parallel tests | devin7788 |
Expand All @@ -116,43 +116,23 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 61 | Making a Type with Built-In Locking (sync.Mutex embedding) | richzw |
| 62 | context.Value is not our friend | hawkinggg |
| 63 | Avoid time.Sleep(), it's not context-aware and can't be interrupted | richzw |
| 64 | Make main() clean and testable. | syjs10 |
| 64 | Make main() clean and testable. | syjs10 |
| 65 | Returning Pointers Made Easy with Generics | miniLCT |
| 66 | Simplify Your Error Messages in fmt.Errorf | smallnest |
| 67 | How to deal with long function signatures | richzw |
| 68 | Use the deadcode tool to find and remove unused functions | richzw |
| 69 | Manage multiple goroutines with errgroup | richzw |
| 70 | Implement a context-aware sleep function | hxzhouh |
| 70 | Implement a context-aware sleep function | hxzhouh |
| 71 | sync.Pool, make it typed-safe with generics | QingyaFan |
| 72 | Case-Insensitive string comparison with strings.EqualFold | syjs10 |
| 73 | Implement String() for enum with the stringer tool | syjs10 |
| 74 | Make time.Duration clear and easy to understand | richzw |
| 72 | Case-Insensitive string comparison with strings.EqualFold | syjs10 |
| 73 | Implement String() for enum with the stringer tool | syjs10 |
| 74 | Make time.Duration clear and easy to understand | richzw |
| 75 | Optimize multiple calls with singleflight | hxzhouh |
| 76 | Result forwarding in function calls | syjs10 |
| 77 | Buffered channels as semaphores to limit goroutine execution | QingyaFan |
| 76 | Result forwarding in function calls | syjs10 |
| 77 | Buffered channels as semaphores to limit goroutine execution | QingyaFan |
| 78 | Non-blocking channel send trick | hxzhouh |
| 79 | | |
| 80 | | |
| 81 | | |
| 82 | | |
| 83 | | |
| 84 | | |
| 85 | | |
| 86 | | |
| 87 | | |
| 88 | | |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | | |
| 93 | | |
| 94 | | |
| 95 | | |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | |
| 100 | | |

## 生成文档

Expand Down
47 changes: 47 additions & 0 deletions src/019.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Tip #19 除非出于文档说明需要,否则避免使用命名结果

> 原始链接:[ Golang Tip #19: Avoid named results unless necessary for documentation.](https://twitter.com/func25/status/1757024709605265674)
>
**注意**:个人而言,我总是避免使用命名结果,因为它们会鼓励使用裸露返回语句。

命名结果可以在源代码及生成的文档(如godoc、pkgsite(http://pkg.go.dev))中增强代码可读性。

但了解何时使用它们至关重要,以下是一些关键要点:

## 必要时进行澄清

**建议**
- 如果函数返回多个同类型值,使用命名结果。
- 若其用途不明显,为清晰起见应为其命名。

![](./images/019/1.png)

**不要**

- 仅仅为了避免在函数内部声明变量而使用命名结果。
- 为了避免反复书写“return nil, err”,而倾向于简单地使用“return”。

## 长函数中避免使用裸露(naked)返回
人们常常对裸露返回持谨慎态度,因为它们可能导致代码可读性降低并影响清晰度。

但在短小的函数中,它们完全是可以接受的:
![](./images/019/2.png)

一眼即可明了其意图,在较长函数中则应避免使用。

为保持代码可读性,与命名结果结合使用时,您有权选择是否使用裸露返回。

## 对于延迟闭包是必要的
若需在延迟函数调用中修改返回值,为结果参数命名至关重要。
![](./images/019/3.png)

此处为结果参数赋予名称 `result``err` 具有特定目的。

这使得这些变量在延迟闭包内可访问,从而根据函数执行结果或针对 panic 进行相应修改。

## 即使在返回多个结果的情况下,有些情况也不必命名
当函数返回相同类型的对象时,特别是在某一类型的成员方法中,为每个返回的对象命名可能会造成冗余,并使我们的文档显得杂乱。

或者,该类型本身可能已经具有自解释性。
![](./images/019/4.png)`
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Tip #16 不要通过返回 -1 或者 nil 来表示错误](./016.md)
- [Tip #17 理解“尽快返回、尽早返回”,避免代码嵌套](./017.md)
- [Tip #18 在使用者的包中定义接口,而不是提供者的包中定义](./018.md)
- [Tip #19 除非出于文档说明需要,否则避免使用命名结果](./019.md)
- [Tip #20 传递值,而不是指针](./020.md)
- [Tip #21 定义方法时,优先使用指针作为接收器](./021.md)
- [Tip #22 使用结构体或变长参数简化函数签名](./022.md)
Expand Down
Binary file added src/images/019/1.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/019/2.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/019/3.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/019/4.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 modified src/images/contributors.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 modified src/images/wechat.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 1dd9aa9

Please sign in to comment.