-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【翻译完成】20200605 Go: Stringer Command, Efficiency Through Code Generation #1837
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
中英文之间、中文与数字之间需要加一个空格
可以参照 中文文案排版指北
https://github.com/studygolang/GCTT/blob/master/chinese-copywriting-guidlines.md
StringerWithSwitch-4 4.99ns ± 1% | ||
``` | ||
常量越多,效率越高。这是有道理的。从内存中加载一个值比一些跳转指令(表示if条件的汇编指令)更具有膨胀性。 | ||
然而,当switch语句越大,跳转指令的数量就越多。从某种程度上来说,从内存中加载将会变得更有效。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当switch语句越大,=> 分支越多
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改
ℹ️ 这篇文章基于 Go 1.13。 | ||
|
||
`stringer`命令的目标是自动生成满足`fmt.Stringer`接口的方法。它将为指定的类型生成`String()`方法,`String()`返回的字符串用于描述该类型。 | ||
`stringer` 命令的目标是自动生成满足 `fmt.Stringer` 接口的方法。它将为指定的类型生成 `String()` 方法, `String()` 返回的字符串用于描述该类型。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
方法, String()
返回的字符串用于描述该类型。
=> 方法,String()
返回的字符串用于描述该类型。
中文标点符号后不需要加空格
|
||
## 例子 | ||
这个[命令的文档](https://godoc.org/golang.org/x/tools/cmd/stringer)中给我们提供了一个学习的例子,如下: | ||
这个[命令的文档](https://godoc.org/golang.org/x/tools/cmd/stringer) 中给我们提供了一个学习的例子,如下: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
## 效率 | ||
`stringer`生成了一个包含每一个字符串的`长字符串`和一个包含每一个字符串索引的数组。在我们这个例子里,读`Aspirin`即是读`长字符串`的索引7到13组成的字符串: | ||
`stringer` 生成了一个包含每一个字符串的 ` 长字符串 ` 和一个包含每一个字符串索引的数组。在我们这个例子里,读 `Aspirin` 即是读 ` 长字符串 ` 的索引 7 到 13 组成的字符串: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
中文与中文之间不需要加空格
``` | ||
➜ go tool compile -S main.go pill_string.go | grep "\"\".Pill\.[^\s]* STEXT" | ||
"".Pill.String STEXT size=275 args=0x18 locals=0x50 | ||
``` | ||
只有`String()`函数被编译到二进制文件中, 自检对性能或二进制大小没有影响。 | ||
只有 `String()` 函数被编译到二进制文件中 , 自检对性能或二进制大小没有影响。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
,
申请校对,关联 #1769