为什么在Go语言中给变量赋值要使用等号?

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

Why does assigning to a variable in Go have an equal sign

问题

我想提前说一下,我只是开始学习和使用Go语言,并且这不是一个语法问题,而是一个关于语言设计的问题。

在Go语言中,假设你有一个int类型的通道c,你可以使用以下方式向通道发送数据:

c <- 1

并且可以使用以下方式从通道接收数据到变量v

v = <- c

我读到过一个简单的记忆语法的方法,即“箭头”指向信息流动的方向,我觉得这很有诗意,尤其作为一个Python的粉丝。我的问题是,为什么不将这种对称的语法完全应用,使得接收通道的语法如下:

v <- c

而不需要等号呢?

我猜想,解释器可能会得到相邻的语法标记,如:

[变量][值]

这可能来自于类似以下语句的表达:

v 1

所以等号的存在允许你重用通常的变量赋值机制,通过使通道接收表达式求值为一个值。不过,让解释器接受没有等号的版本是否困难呢?实际上,它只需要将其视为该情况下的二元操作符即可。

这似乎还导致了其他情况,如果有两个通道c1c2,你可以使用以下语法:

c2 <- <- c1

从一个通道读取数据并将其传输到另一个通道。


我还想说的是,我并不是在寻求关于代码风格的意见,而是试图理解Go语言的设计决策背后的原因。

英文:

I would like to say upfront that I'm only starting to learn about and use the Go language and also that this isn't a syntax question but more of a language design question.

In Go, assuming that you have a channel c of int, you can send on the channel using:

c &lt;- 1

And receive from the channel into a variable v with:

v = &lt;- c

I've read that a simple way to remember the syntax is that the "arrow" points in the direction in which information is flowing, which I think is poetic, especially being quite the Python fan. My question is why is this not taken all the way so you have the symmetric syntax:

v &lt;- c

in order to receive from a channel? Why have the equals sign there?

I suppose the interpreter would end up with adjacent syntax tokens like:

[variable][value]

which could conceivably come from a statement like:

v 1 

So that the equals sign there allows you to reuse the usual variable assignment machinery by making that channel receipt evaluate to a value. Would it be that difficult to make the interpreter accept the version without the equals sign though? It would basically just have to treat it as a binary operator for that case.

It also seems to lead to other cases where if there are two channels c1 and c2 you have the syntax:

c2 &lt;- &lt;- c1

In order to read from one and transmit it into the other.

<hr>

I'd like to also say that I'm not trying to elicit opinions on style but rather trying to understand the decisions drove Go to be the way it is.

答案1

得分: 2

将两个分别执行不同操作的正交操作符组合在一起,可以更加表达丰富。

除了@sedat-kapanoglu提供的示例之外,想象一下如何表达以下内容:

x := <-ch
x += <-ch
x, y = <-ch1, <-ch2

另外,根据你的建议,以下表达式:

y <- x

根据变量x和y的类型,可以是从通道x读取,也可以是向通道y写入。与使用现有语法的代码进行比较:

y = <-x
y <- x
英文:

Combining two orthogonal operators each doing one thing is much more expressive.

In addition to the examples provided by @sedat-kapanoglu, think how would you express the following:

x := &lt;-ch
x += &lt;-ch
x, y = &lt;-ch1, &lt;-ch2

Also with your proposal the following expression:

y &lt;- x

could be a read from a channel x or a write to a channel y depending on the types x and y. Compare it to the code that uses the existing syntax:

y = &lt;-x
y &lt;- x

huangapple
  • 本文由 发表于 2015年7月10日 08:35:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/31330610.html
匿名

发表评论

匿名网友

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

确定