Are x[i] or x.f considered "variables"?

blov · · 410 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><em>The Go Programming Language</em> has this interesting sentence in Section 2.3.2: &#34;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>.&#34;</p> <p>In other languages, I&#39;m used to <strong>x[i]</strong> being referred to with terms like &#34;array element&#34;, not &#34;variable&#34;. I&#39;m used to <strong>x.f</strong> being referred to with terms like &#34;field&#34; or &#34;attribute&#34;, not &#34;variable&#34;. 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 &#34;variable&#34; applies to anything which can be assigned a value. </p> <p>I&#39;d say its more semantics than anything, though; the distinction isn&#39;t horribly important unless you&#39;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&#39;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

410 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传