Go Proposal: Spec: Add Range Over Int, Range Over Func
The article shows a proposal to add two new types that a for-range statement can range over in the Go programming language: integers and functions. The proposal suggests that if n is an integer type, then for x := range n { ... }
would be equivalent to for x := T(0); x < n; x++ { ... }
, where T is the type of n. The proposal also introduces range over functions, where for x, y := range f { ... }
is similar to f(func(x T1, y T2) bool { ... })
, with the loop body moved into the function literal passed to f as yield. The boolean result from yield indicates whether to keep iterating. The article argues that the combination of range over integers and range over functions should make range the preferred form of the for loop for most iterations. The proposal aims to simplify code and make it easier to understand. This addition to the language would provide developers with more flexibility and options when it comes to iterating over integers and functions.