Alex Rivera | Logout

Make a struct "range-able"?

Asked 2012-10-15T11:08:26.020
11
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?

go
Edit
Report

1 Answer

14

Has Friends to be a struct? Otherwise simply do

type Friends []Friend
answered 2012-10-15T13:45:28.383

Your Answer