Go语言中的分配运算符 [英] Assignment operator in Go language
本文介绍了Go语言中的分配运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最近我正在玩Google的新编程语言转到
并想知道为什么赋值运算符:=
在等号前面有一个冒号 =
是否存在特定为什么这个语言的作者想要使用 name:=John
而不是 name =John
解决方案
:=
符号既可以作为声明,也可以作为初始化。
foo:=bar
相当于
var foo =bar
为什么不使用 foo =bar
就像在任何脚本语言中一样可能会问?
foo =bar
fooo =baz+ foo +baz //哎呀,是fooo一个新变量还是我的意思是'foo'?
Lately I was playing with google's new programming language Go
and was wondering why the assignment operator :=
has a colon in front of the equal sign =
Is there a particular reason why the authors of the language wanted to use name := "John"
instead of name = "John"
解决方案
The :=
notation serves both as a declaration and as initialization.
foo := "bar"
is equivalent to
var foo = "bar"
Why not using only foo = "bar"
like in any scripting language, you may ask ? Well, that's to avoid typos.
foo = "bar"
fooo = "baz" + foo + "baz" // Oops, is fooo a new variable or did I mean 'foo' ?
这篇关于Go语言中的分配运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文