Skip to content

Commit

Permalink
add #82
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed May 4, 2024
1 parent f8e16d4 commit 0549aa7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 79 | If doing something unusual, comment why | smallnest |
| 80 | Declare variables NEAR their usage | smallnest |
| 81 | To check if a string is empty, prefer s != "" over len(s) == 0 | smallnest |
| 82 | Enhance Debug Output with %+v and %q Instead of %v and %s | smallnest |

## 生成文档

Expand Down
2 changes: 1 addition & 1 deletion src/081.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tip #81 在检查一个字符串是否为空时,推荐使用 `s != ""` 而不是 `len(s) == 0`

> 原始链接:[ Golang Tip #81: To check if a string is empty, prefer s != "" over len(s) == 0.](https://twitter.com/func25/status/1785609616472379539)
> 原始链接:[Golang Tip #81: To check if a string is empty, prefer s != "" over len(s) == 0](https://twitter.com/func25/status/1785609616472379539)
>
两种解决方案都是完全可以的,并且这两种技术在Go的标准库中都很常见。
Expand Down
41 changes: 41 additions & 0 deletions src/082.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Tip #82 使用`%+v``%q`来增强调试输出,而不是使用`%v``%s`

> 原始链接:[Golang Tip #82: Enhance Debug Output with %+v and %q Instead of %v and %s](https://twitter.com/func25/status/1786377258082746722)
>
当我们进行调试时,能够从日志中获得更清晰、更详细的输出对我们来说这会是一个巨大的帮助。

举一个常见的场景,你正在用`fmt.Println`快速检查一个结构体的状态:

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


这里输出 `{30 15 5}` 并不会立即告诉我们哪个数字代表胜利`(Wins)`,哪个代表失败`(Losses)`,或者哪个代表平局`(Draws)`

除非你记得在结构体中定义的顺序。

但是,通过使用 `%+v``%#v`(它们确实是 `%v` 的变体),我们可以使输出更加具有信息量和易于理解:

![](./images/082/2.png)

- `%+v` 非常好,因为它在输出中直接包含了字段名,消除了每个值代表什么的任何混淆。
- `%#v` 更进一步,显示了包含包名和结构体类型的完整结构。这可能比你日常需要的细节更多,但在更复杂的场景中会非常有用。

> “%q 怎么样?”
`%q` 本质上是一个将字符串用引号括起来的快捷方式,而 `%s` 只是原样输出字符串。

这种区别在处理以下情况时特别有用:
- 可能是空的字符串
- 或者包含特殊字符或空格,这些字符不是立即可见的:

![](./images/082/3.png)

只要看一下使用每种格式化选项生成的日志,就会注意到当使用 `%q` 时,问题变得多么清晰?

> “但是有什么缺点呢?”
不过,有一点需要注意,那就是 `%q` 不会解释像 `\t``\n` 这样的转义字符,这可能会使字符串更难阅读。

(实际上,这可能非常有用,因为它会向你展示字符串中确切的内容,保留所有的特殊字符。)

3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@
- [Tip #78 非阻塞 channel 发送技巧](./078.md)
- [Tip #79 如果做了不寻常的事,请说明原因](./079.md)
- [Tip #80 在其使用位置附近声明变量](./080.md)
- [Tip #81 在检查一个字符串是否为空时,推荐使用 s != "" 而不是 len(s) == 0](./081.md)
- [Tip #81 在检查一个字符串是否为空时,推荐使用 s != "" 而不是 len(s) == 0](./081.md)
- [Tip #82 使用 %+v 和 %q 来增强调试输出,而不是使用 %v 和 %s](./082.md)
Binary file added src/images/082/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/082/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/082/3.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 0549aa7

Please sign in to comment.