Go update slice iterating error "does not support indexing"

Issue

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)

Solution

Type *Members does not support indexing, type Members does.

m := make([]Members, 5)
pm := &m

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

http://play.golang.org/p/_26TUBHdDm

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 By – Stephen Weinberg

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.