You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Questions like this are better addressed by the golang-nuts mailing list, but the short summary is the Go programming language specification says the assignment getArray()[0] = 1 is not allowed.
Assignments: "Each left-hand side operand must be addressable, a map index expression, or (for = assignments only) the blank identifier."
Address operators: "The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array."
getSlice()[0] is a slice indexing operation, so it's addressable, so the assignment is valid.
getPointerToArray()[0] is short-hand for (*getPointerToArray())[0], and pointer indirections are addressable, so this is an array indexing of an addressable array.
getArray()[0], however, is an array indexing of a non-addressable array, so the Go compiler is rightfully rejecting the statement as invalid.
I' m doubting about the code below:
both "getSlice()[0] = 1" and "getPointerToArray()[0] = 1" are ok for compiler.
but getArray()[0] can not go through.
The go 1.5 compiler give error:
"cannot assign to getArray()[0]"
Is this a problem?if not, Who could tell me why we can't not use the form like above! 3ks!
The text was updated successfully, but these errors were encountered: