SO: really good answer to golang pointer question
Go update slice iterating error "does not support indexing"

                

    
    
                
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.

package main

import "fmt"

type Member struct {
    Name, Surname string
    Age           int
}
type Members []Member

func main() {
    members1 := Members{
        {"Bob", "Doe", 30},
        {"Jack", "Doe", 30},
        {"John", "Doe", 30},
    }
    members1.ModifyName()
    members1.ModifyAllNames1()
    members1.ModifyAllNames2()
    fmt.Println(members1)
}

func (members *Members) ModifyName() {

    *members[0].Name = "-"
}

func (members *Members) ModifyAllNames1() {

    for i := 0; i < len(*members); i++ {
        *members[i].Name = "-"
    }
}

func (members *Members) ModifyAllNames2() {

    for _, m := range *members {
        m.Name = "-"
    }
}   


Then I get the following errors:

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

    

        

    
    
        
            asked Aug 13, 2014 at 16:03
        
        
    
    
        
    
    

    





                
                
                
                    


                                    

        

        


    
    
Type *Members does not support indexing, type Members does.
m := make([]Members, 5)
pm := &m

m[:2]     // good
pm[:2]    // bad
(*pm)[:2] // good


https://go.dev/play/p/CDaBKqB-sqM
package main

import "fmt"

type Member struct {
    Name, Surname string
    Age           int
}
type Members []Member

func main() {
    members1 := Members{
        {"Bob", "Doe", 30},
        {"Jack", "Doe", 30},
        {"John", "Doe", 30},
    }
    members1.ModifyName()
    members1.ModifyAllNames1()
    //members1.ModifyAllNames2()
    fmt.Println(members1)
}

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

func (members *Members) ModifyAllNames1() {
    for i := 0; i < len(*members); i++ {
        (*members)[i].Name = "-"
    }
}

func (members Members) ModifyAllNames2() {
    for i := range members {
        members[i].Name = "-"
    }
}

    
    
    
        
            answered Aug 13, 2014 at 16:05
        
        
    
    
        
    
    
        Stephen WeinbergStephen Weinberg53.2k14 gold badges140 silver badges117 bronze badges
        
    

    





            5
    


                                    

    
    

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.
    
    
            
            
        
    


            
    
        
            answered Jun 30, 2018 at 0:36
        
        
    
    
        
    
    
        cnstcnst27.1k7 gold badges97 silver badges128 bronze badges
        
    

        
    



                        


                            
                                
                            
                
            