类型名称周围的括号

huangapple go评论95阅读模式
英文:

Parenthesis around type names

问题

我喜欢在Go语言中将类型名称与变量进行一些视觉上的分离。

我一直在尝试以下代码:

var target (int64) = 600851475143
var largest (int64) = 0
var i (int64)

到目前为止,它可以正确编译,并且在程序运行结果上我没有看到任何差异。

这样做是否有风险?上述代码与下面的代码有什么语义上的区别吗?

var target int64 = 600851475143
var largest int64 = 0
var i int64

谢谢。

英文:

I like having a little visual separation of the type names from the variables in Go.
I've been playing with the following:

var target (int64) = 600851475143
var largest (int64) = 0
var i (int64)

So far it compiles correctly and I don't see any difference in the result of my program run.

Is this dangerous? Is there any semantic difference between the above and the below?

var target int64 = 600851475143
var largest int64 = 0
var i int64

Thanks

答案1

得分: 8

没有语义上的区别,但你可能会发现自己在与go fmt作斗争,所以我认为这不值得。

英文:

There is no semantic difference but you may find yourself fighting against go fmt so I'm thinking it's not worth it.

答案2

得分: 4

你不是一个人在编码。

即使没有其他人阅读你的代码,你也会阅读其他人的代码:标准API的代码,你将使用的组件的代码,以及博客、SO答案和所有文档的代码。

由go引入并由gofmt强制执行的格式标准化确保你可以轻松阅读所有的go代码,而不需要适应作者的格式风格。在简单格式化的代码中,很多错误自然而然地被避免,因为不常见的错误会立即被发现。

此外,在标准编辑器中,你的代码看起来就像没有加括号一样:

类型名称周围的括号

不需要添加任何内容来使类型可见。

我建议你对所有的代码运行gofmt,并阅读标准包的代码,以尝试使用相同的格式风格(包括注释)。

这是Rob Pike关于代码和注释装饰的观点。我认为这些都是很好的建议,即使不是所有的都适用于go。

英文:

You don't code alone.

Even if nobody else ever reads your code, you'll read the code of others : the one of the standard API, the one of the components you'll use, and the one of the blogs, SO answers and all documentations.

The standardization of formatting brought by go and enforced by gofmt ensures you read all go code easily, without having to get used to the formatting style of the writer. In a code simply formatted, a lot of errors are naturally avoided as what isn't usual is immediately seen.

Besides, here's like your not parenthesis enhanced code look like in a standard editor :

类型名称周围的括号

No need to add something to let the types be visible.

I suggest you run gofmt on all your code and read the standard packages code in order to try to have the same formatting style (including for the comments).

Here's what Rob Pike had to say about code and comment decoration. I think those are good advices even if not everything apply to go.

huangapple
  • 本文由 发表于 2012年7月11日 05:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/11422082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定