<p><em>The Go Programming Language</em> has this interesting sentence in Section 2.3.2: "Variables created by declarations are identified by a name, such as <strong>x</strong>, but many variables are identified only by expressions like <strong>x[i]</strong> or <strong>x.f</strong>."</p>
<p>In other languages, I'm used to <strong>x[i]</strong> being referred to with terms like "array element", not "variable". I'm used to <strong>x.f</strong> being referred to with terms like "field" or "attribute", not "variable". So is this term even correct? Is this distinction important in Go?</p>
<hr/>**评论:**<br/><br/>lstokeworth: <pre><p>Go uses the term variable to refer to a storage location. Slice elements and struct fields are storage locations.</p></pre>012a: <pre><p>Technically yes; in Go, the term "variable" applies to anything which can be assigned a value. </p>
<p>I'd say its more semantics than anything, though; the distinction isn't horribly important unless you're planning on contributing to the language lexer. </p></pre>alasijia: <pre><p>No for a map or string variable <strong>x</strong>, <strong>x[i]</strong> is not addressable, so <strong>x[i]</strong> can't be viewed as variable.</p></pre>dchapes: <pre><p>Incorrect. In computer science variables are just storage locations (memory) with names (identifiers, including <code>x[i]</code>), they need not be mutable (e.g. a <code>const</code> variable in C++).</p></pre>alasijia: <pre><p>go spec says </p>
<blockquote>
<p>A variable is a storage location for holding a value.</p>
</blockquote>
<p>.</p>
<blockquote>
<p>Structured variables of array, slice, and struct types have elements and fields that may be addressed individually. Each such element acts like a variable. </p>
</blockquote>
<p>It neither deny my opinion nor your opinion directly, but I feel it prefers to mine, :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传