Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang how to range in pointer array

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

like image 843
Umutcan Ertürk Avatar asked May 16 '26 17:05

Umutcan Ertürk


2 Answers

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.

like image 63
RayfenWindspear Avatar answered May 19 '26 10:05

RayfenWindspear


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

like image 22
william.taylor.09 Avatar answered May 19 '26 10:05

william.taylor.09



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!