type Friend struct {
name string
age int
}
type Friends struct {
friends []Friend
}
I'd like to make Friends
range-able, that means, if I have a variable my_friends
with type Friends
, I can loop though it with:
for i, friend := range my_friends {
// bla bla
}
is it possible in Go?
Has Friends to be a struct? Otherwise simply do
type Friends []Friend
Beware: As deft_code mentions, this code leaks a channel and a goroutine when the loop breaks. Do not use this as a general pattern.
In go, there is no way to make an arbitrary type compatible for range
, as
range
only supports slices, arrays, channels and maps.
You can iterate over channels using range
, which is useful if you want to iterate over dynamically generated data without having to use a slice or array.
For example:
func Iter() chan *Friend {
c := make(chan *Friend)
go func() {
for i:=0; i < 10; i++ {
c <- newFriend()
}
close(c)
}()
return c
}
func main() {
// Iterate
for friend := range Iter() {
fmt.Println("A friend:", friend)
}
}
That's the closest thing you can have to make something 'rangeable'.
So a common practice is to define a method Iter()
or something similar on your type and
pass it to range
.
See the spec for further reading on range
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With