在Go语言中的同时赋值

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

Simultaneous assignment in Go

问题

我正在学习Go语言,有一件事我不太明白,为什么这门语言的创造者支持同时赋值?这样很容易出错,比如 a, b = a, b 而不是 a, b = b, a,我希望能得到一些好的解释,提前感谢。

英文:

I'm learning Go and can't understand one thing, why creators of this language do support simultaneous assignment? It is very easy to make mistakes like a, b = a, b and not a, b = b, a, as I would want, thanks in advance for any good explanations.

答案1

得分: 13

如果没有同时赋值的功能,那么你就需要采取其他方法。另一种替代方案可能是这样的:

tmp = a
a = b
b = tmp

这样做容易出错。

英文:

> It is very easy to make mistakes like a, b = a, b and not a, b = b, a,

If simultaneous assignment were not available then you would have to do something else instead. An alternative approach might look something like this:

tmp = a
a = b
b = tmp

That's much easier to get wrong.

答案2

得分: 8

你还能以什么方式获取函数的第二、第三、第四,以及更多的返回值呢?

英文:

How else would you get access to the second, third, fourth, … return value of a function?

huangapple
  • 本文由 发表于 2010年12月26日 07:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/4532095.html
匿名

发表评论

匿名网友

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

确定