9

if I have PersonManager struct and it has *[]Person array. I want to range every item in this array. For example

manager := *PersonManager

for ii := 0; len(*manager.allPersons); ii++{
    fmt.Println(manager.allPersons[:ii].name)
}

for this example, manager variable is pointer and array that in this variable is pointer too. How can I range this items?

Not: I m getting cannot slice manager.allPersons (type *[]Person) error

2 Answers 2

12

Use the range keyword. https://github.com/golang/go/wiki/Range

for i, person := range *manager.allPerson {
    fmt.Println(person.name)
}

Note that the compiler will be angry if you don't use the index var i. If you don't intend to use it, replace with _.

To further explain, your original error was due to the fact that you correctly dereferenced your *[]Person the first time, but not inside the for loop.

fmt.Println(manager.allPersons[:ii].name) // wrong
fmt.Println((*manager.allPersons)[:ii].name) // right

Also, given that your slice contains Person struct values, it will have to copy it if you use the optional second value in the range expression. Thus, it would be more efficient to keep using just the indexes.

As a suggestion, don't use a *[]Person, use []*Person, which is probably what you intended to use anyway. A slice is already a pointer value. Using []*Person you don't have to fear a copy by the range expression because it is simply a pointer to a Person instead of the entire struct.

In fact, unless you either change the type to []*Person or index into the slice you won't be able to have changes reflected in the slice because a struct value will be a copy in the range loop. See https://github.com/golang/go/wiki/Range#gotchas but be aware that if the value is a pointer value, this isn't an issue.

Sign up to request clarification or add additional context in comments.

6 Comments

Couple things; the OP has more issues than just the pointer. Also, you declared i in this scope, but don't use it, which would cause a compilation error.
@william.taylor.09 Left i in there so I could explain its use.
Good call. As a followup (and it's going to sound nitpicky), it's more idiomatic to just use "for i := range *manager.allPerson", In your example, you're going to force Go to create a variable that would never be used. Nitpicky, but just commenting for those in the future to see.
@william.taylor.09 was just about to address this, as well as explain his original error.
Cheers, great revised explanation.
|
2

I think you might be mixed up by the pointers. You don't need to dereference your pointer anywhere. Something like this should work:

for i := 0; i < len(manager); i++ {
    fmt.Println(manager[i].Name)
}

GoPlay here: https://play.golang.org/p/P8wAp4wIGs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.