xxjwxc / uber_go_guide_cn Goto Github PK
View Code? Open in Web Editor NEWUber Go 语言编码规范中文版. The Uber Go Style Guide .
License: Apache License 2.0
Uber Go 语言编码规范中文版. The Uber Go Style Guide .
License: Apache License 2.0
不知如何理解
Channel 的指南
原文“channel 通常 size 应为 1 或是无缓冲的。默认情况下,channel 是无缓冲的,其 size 为零。任何其他尺寸都必须经过严格的审查。考虑如何确定大小,是什么阻止了 channel 在负载下被填满并阻止写入,以及发生这种情况时发生了什么。”
疑问:“是什么阻止了 channel 在负载下被填满并阻止写入” 阻止了被填满 阻止写入,是什么意思。。。
在处理时间的瞬间时使用 time.time,在比较、添加或减去时间时使用 time.Time 中的方法。
应该是
在处理时间的瞬间时使用 time.Time,在比较、添加或减去时间时使用 time.Time 中的方法。
type LogHandler struct {
h http.Handler
log *zap.Logger
}
var _ http.Handler = LogHandler{}
func (h LogHandler) ServeHTTP(
w http.ResponseWriter,
r *http.Request,
) {
// ...
}
上述示例代码中的 var _ http.Handler = LogHandler{}
这一句 应该是 var _ http.Handler = &LogHandler{}
吧,因为要验证是否实现接口,应该是取LogHandler struct的指针进行赋值。 放到Goland等IDE中这行应该直接会飘红了。
指定Map容量提示
和 尽量初始化时指定 Map 容量
链接的内容重复
进一步的分析和描述
sPtrs := map[int]*S{1: {"A"}} 确定编译器不会报错吗?
“样式 (style) 是支配我们代码的惯例。”
建议style翻译为“风格”。风格隐含着一个人的思维、文化、知识等因素对其行为、偏好的影响,从而在他的作品中表现出与众不同的特征和性状。
文学家有文风、画家有画风、代码专家当然也有码风咯~
You almost never need a pointer to an interface. You should be passing interfaces as values—the underlying data can still be a pointer.
An interface is two fields:
If you want interface methods to modify the underlying data, you must use a pointer.
读英文时,我猜他的本意应该是表达两点:
you must use a pointer.这个pointer应该是指给接口变量赋值时的对象指针.
我这样理解对吗
Channel Size is One or None
Channels should usually have a size of one or be unbuffered. By default, channels are unbuffered and have a size of zero. Any other size must be subject to a high level of scrutiny. Consider how the size is determined, what prevents the channel from filling up under load and blocking writers, and what happens when this occurs.
推荐用机器翻译的非常好理解,人工翻译补充的语气词画蛇添足,让人误解甚至是错误。这个规范更多是提醒,慎重和建议
百度翻译
通道大小为一或无
通道通常应具有一个或无缓冲的大小。默认情况下,通道是无缓冲的,大小为零。任何其他尺寸都必须经过高度审查。考虑大小是如何确定的,是什么阻止通道在负载下填满并阻塞写入程序,以及发生这种情况时会发生什么。
谷歌翻译
通道大小为一或无
通道的大小通常应为 1 或无缓冲。默认情况下,通道是无缓冲的,大小为零。任何其他尺寸都必须经过严格审查。考虑如何确定大小,防止通道在负载和阻塞写入器下填满的原因,以及发生这种情况时会发生什么。
Channel 的 size 要么是 1,要么是无缓冲的
channel 通常 size 应为 1 或是无缓冲的。默认情况下,channel 是无缓冲的,其 size 为零。任何其他尺寸都必须经过严格的审查。我们需要考虑如何确定大小,考虑是什么阻止了 channel 在高负载下和阻塞写时的写入,以及当这种情况发生时系统逻辑有哪些变化。(翻译解释:按照原文意思是需要界定通道边界,竞态条件,以及逻辑上下文梳理)
最后“功能选项”错误,opts.Logger = l.Log 应该为 opts.logger = l.Log
Bad 示例代码中注释翻译有误:“f.Close 将会被执行”。应该是:f.Close 将不会被执行。
下面这个例子,我使用 go vet 命令,并没有提示异常,这个规范是不是要自定义,Uber 有把 vettool 开源出来吗?
使用字段名初始化结构体
初始化结构体时,应该指定字段名称。现在由 go vet 强制执行。
Bad | Good |
---|---|
k := User{"John", "Doe", true} |
k := User{
FirstName: "John",
LastName: "Doe",
Admin: true,
} |
你好, 感谢你的翻译, 这对我很有帮助, 我在查看过程中发现 https://github.com/xxjwxc/uber_go_guide_cn#%E9%94%99%E8%AF%AF%E5%8C%85%E8%A3%85 处的表述与原文不一致,
原文是 https://github.com/uber-go/guide/blob/master/style.md#error-wrapping
return the original error as-is
add context with fmt.Errorf and the %w verb
add context with fmt.Errorf and the %v verb
意思是
将原始错误原样返回
使用 fmt.Errorf 搭配 %w 将错误添加进上下文后返回
使用 fmt.Errorf 搭配 %v 将错误添加进上下文后返回
而 master 的描述是
按原样返回原始错误
add context with fmt.Errorf and the %w verb
使用fmt.Errorf和%w
使用 fmt.Errorf 和 %v
这可能会造成误解
Channel 的 size 要么是 1,要么是无缓冲的。这似乎很理想,然而大小为64和大小为1的channel,在工程实践上有又有多大的区别呢?
type F interface {
f()
}
type S1 struct{}
func (s S1) f() {}
type S2 struct{}
func (s *S2) f() {}
// f1.f()无法修改底层数据
// f2.f() 可以修改底层数据,给接口变量f2赋值时使用的是对象指针
var f1 F:= S1{}
var f2 F:= &S2{}
var f1 F = S1{}
var f2 F = &S2{}
newDay := t.AddDate(0 /* years */, 0, /* months */, 1 /* days */)
应该是
newDay := t.AddDate(0 /* years */, 0 /* months */, 1 /* days */)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.