Git Product home page Git Product logo

uber_go_guide_cn's Issues

对于channel的疑问

不知如何理解
Channel 的指南
原文“channel 通常 size 应为 1 或是无缓冲的。默认情况下,channel 是无缓冲的,其 size 为零。任何其他尺寸都必须经过严格的审查。考虑如何确定大小,是什么阻止了 channel 在负载下被填满并阻止写入,以及发生这种情况时发生了什么。”

疑问:“是什么阻止了 channel 在负载下被填满并阻止写入” 阻止了被填满 阻止写入,是什么意思。。。

翻译校正

在处理时间的瞬间时使用 time.time,在比较、添加或减去时间时使用 time.Time 中的方法。
应该是
在处理时间的瞬间时使用 time.Time,在比较、添加或减去时间时使用 time.Time 中的方法。

Interface合理性验证

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中这行应该直接会飘红了。

【翻译建议】样式 (style) 是支配我们代码的惯例。

“样式 (style) 是支配我们代码的惯例。”

建议style翻译为“风格”。风格隐含着一个人的思维、文化、知识等因素对其行为、偏好的影响,从而在他的作品中表现出与众不同的特征和性状。

文学家有文风、画家有画风、代码专家当然也有码风咯~

Pointers to Interfaces 的意图

Pointers to Interfaces

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:

  1. A pointer to some type-specific information. You can think of this as "type."
  2. Data pointer. If the data stored is a pointer, it’s stored directly. If the data stored is a value, then a pointer to the value is stored.

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

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 在高负载下和阻塞写时的写入,以及当这种情况发生时系统逻辑有哪些变化。(翻译解释:按照原文意思是需要界定通道边界,竞态条件,以及逻辑上下文梳理)

请问 Uber 的 go vet 是不是自定义的

下面这个例子,我使用 go vet 命令,并没有提示异常,这个规范是不是要自定义,Uber 有把 vettool 开源出来吗?

使用字段名初始化结构体

初始化结构体时,应该指定字段名称。现在由 go vet 强制执行。

BadGood
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

这可能会造成误解

书写错误var f1 F := S1{}

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 */)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.