Go - Preview: ranging over functions in Go

2023/07/22
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

A major change proposal for the Go language has been published, suggesting the addition of the ability to range over functions. This proposal is expected to be included in a future Go release. The article explains the motivation behind this proposal and provides examples of how it would work.

Currently, the for-range loop in Go is limited to arrays, slices, strings, maps, and channels. The proposed change would allow developers to iterate over integers as well. The article provides a basic example of how this would look, showing that it is a clean shortcut for a common loop.

The more interesting part of the proposal is the ability to range over functions. This addresses a limitation in Go when it comes to iterating over elements in custom containers or Abstract Data Structures. Previously, programmers had to come up with ad-hoc iteration methods, but with this proposal, they would be able to use the for-range loop more naturally.

The article highlights the usefulness of this change, especially for programmers who have been working with generics in Go. It mentions that the proposed change is simple and clear, and approximately half of the 3-clause for loops observed in the wild can be converted to the "range over int" form.

Overall, this proposal aims to enhance the versatility and convenience of the Go language, making it easier for developers to iterate over different types of data structures.