Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range over an arbitrary type

Tags:

iterator

range

go

Is there a way to make an arbitrary type range-able in Go? For example, Python offers __iter__(), which is really useful. I tried searching for the answer but I got no results.

like image 326
postman Avatar asked May 08 '26 21:05

postman


1 Answers

You've searched successfully, there's no support for ranging over arbitrary types in Go.

From the specs:

RangeClause = ( ExpressionList "=" | IdentifierList ":=" ) "range" Expression .

The expression on the right in the "range" clause is called the range expression, which may be an array, pointer to an array, slice, string, map, or channel permitting receive operations.

like image 147
zzzz Avatar answered May 10 '26 21:05

zzzz