Skip to content

Commit

Permalink
Merge pull request #147 from jjjjjim/master
Browse files Browse the repository at this point in the history
Tip #35 转换字符串时优先使用 strconv 而非 fmt
  • Loading branch information
smallnest authored Apr 24, 2024
2 parents e3fef80 + 70a8701 commit d1313af
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 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).
| 32 | | |
| 33 | Just... Don’t Panic() | baxiang |
| 34 | | |
| 35 | | |
| 35 | Prefer strconv over fmt for converting to/from string | jjjjjim |
| 36 | | |
| 37 | | |
| 38 | Make your errors clear with fmt.Errorf, don't just leave them bare | smallnest |
Expand Down Expand Up @@ -201,6 +201,7 @@ http://localhost:3000
<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="Code">💻</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="Code">💻</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="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jjjjjim"><img src="https://avatars.githubusercontent.com/u/87107430?v=4?s=100" width="100px;" alt="jjjjjim"/><br /><sub><b>jjjjjim</b></sub></a><br /><a href="https://github.com/smallnest/gotips/commits?author=jjjjjim" title="Code">💻</a></td>
</tr>
</tbody>
<tfoot>
Expand Down
25 changes: 25 additions & 0 deletions src/035.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Tip #35 转换字符串时优先使用 strconv 而非 fmt

> 原始链接:[Golang Tip #35: Prefer strconv over fmt for converting to/from string.](https://twitter.com/func25/status/1763187911942660330)
>
当需要将数字转换为字符串时,选择合适的工具可以加快处理速度。

**strconv** 包专门为这个场景而设计,每一点性能提升和内存节省都很重要。

我们来看一个简单的基准测试:

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

基准测试显示出显着的性能差异。

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

*(虽然我不确定编译器是否做了优化,但两者的上下文是相同的)*

- **strconv** 的函数是为特定的转换任务设计的,这使得它们能比更通用的 **fmt** 函数执行得更快。
- **fmt.Sprint** 函数及其变体需要通过反射来识别其正在处理的类型,并确定如何将其格式化为字符串。

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

这个反射过程并非无成本,它既增加了时间也增加了内存开销。
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [Tip #30 使用context.WithoutCancel()继续上下文操作](./030.md)
- [Tip #31 使用跳转标签让break和continue语句更简洁](./031.md)
- [Tip #33 尽量...不要使用panic()](./033.md)
- [Tip #35 转换字符串时优先使用 strconv 而非 fmt](./035.md)
- [Tip #38 使用 fmt.Errorf 使你的错误信息清晰明了,不要让它们过于赤裸](./038.md)
- [Tip #39 避免在循环中使用defer,否则可能会导致内存溢出](./039.md)
- [Tip #40 在使用defer时处理错误以防止静默失败](./040.md)
Expand Down
Binary file added src/images/035/035_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/035/035_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/035/035_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 d1313af

Please sign in to comment.