Go update slice iterating error “does not support indexing”

后端 未结 2 1672
暖寄归人
暖寄归人 2021-02-02 08:01

I\'m trying to iterate through slice values and update one or more values, accessing it by index or iterating through them. Then I have the following problem.

pa         


        
2条回答
  •  孤独总比滥情好
    2021-02-02 08:45

    func (members *Members) ModifyName() {
        *members[0].Name = "-"
    }
    

    invalid operation: members[0] (type *Members does not support indexing)

    The issue is that of operator associativity and/or precedence, although I couldn't find this specific case explicitly covered within the Golang spec.

    The expression *members[0].Name appears to be equivalent to *(members[0].Name), so, to get the proper pointer deference, you gotta use a pair of parentheses explicitly around the item that must be dereferenced, as is, (*members)[0].Name.

提交回复
热议问题