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:
012a:Go uses the term variable to refer to a storage location. Slice elements and struct fields are storage locations.
alasijia: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.
dchapes:No for a map or string variable x, x[i] is not addressable, so x[i] can't be viewed as variable.
alasijia:Incorrect. In computer science variables are just storage locations (memory) with names (identifiers, including
x[i]
), they need not be mutable (e.g. aconst
variable in C++).
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, :)
