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

blov · 2017-01-16 01:00:30 · 513 次点击    
这是一个分享于 2017-01-16 01:00:30 的资源,其中的信息可能已经有所发展或是发生改变。

The Go Programming Language has this interesting sentence in Section 2.3.2: "Variables created by declarations are identified by a name, such as x, but many variables are identified only by expressions like x[i] or x.f."

In other languages, I'm used to x[i] being referred to with terms like "array element", not "variable". I'm used to x.f being referred to with terms like "field" or "attribute", not "variable". So is this term even correct? Is this distinction important in Go?


评论:

lstokeworth:

Go uses the term variable to refer to a storage location. Slice elements and struct fields are storage locations.

012a:

Technically yes; in Go, the term "variable" applies to anything which can be assigned a value.

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.

alasijia:

No for a map or string variable x, x[i] is not addressable, so x[i] can't be viewed as variable.

dchapes:

Incorrect. In computer science variables are just storage locations (memory) with names (identifiers, including x[i]), they need not be mutable (e.g. a const variable in C++).

alasijia:

go spec says

A variable is a storage location for holding a value.

.

Structured variables of array, slice, and struct types have elements and fields that may be addressed individually. Each such element acts like a variable.

It neither deny my opinion nor your opinion directly, but I feel it prefers to mine, :)


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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