I have a type List []string
and I am implementing some standard functions like Insert
, DeleteAt
etc. I would like to implement range
so I can iterate the list easily.
I cannot seem to find the way to do that.
There is no reason to re-implement range since the range keyword will work on type List.
var l List
for i, v := range l {
/* whatever */
}
It is not possible in Go to implement range yourself for a given type. Range only works for Go's built-in data structures: slices, maps and channels (and arrays?).