-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Go 2: additional kinds of range loops #36308
Comments
There’s a package that allows you do to exactly this: github.com/bradfitz/iter. |
Seems like we can do that today using
A bit awkward, yes, but if we're going to extend for/range it's not clear that these are the specific steps we want to take. |
How often got this package by various other packages imported? |
IIRC Python's Personally, I prefer the traditional C-style for v := 0; v < 25; v++ {} It's always clear what it's doing and, if the need arises, it has various fancy tricks up its sleeve such as multiple iteration variables with different steps. |
Re multiple iteration variables #30917 |
It would be good to provide a step option too:
|
Some language including BASIC does have a
|
@proyb6 looks great. Fine by me. I would go with that as well, but range should be included |
How does Swift do it? |
Like this: // includes end point, implicit step of 1
for i in 0...25 {}
// excludes end point, implicit step of 1
for i in 0..<25 {}
// includes end point, explicit step of 2
for i in stride(from: 0, through: 25, by: 2) {}
// excludes end point, explicit step of 2
for i in stride(from: 0, to: 25, by: 2) {} For some unfathomable reason, they got rid of the traditional C-style EDIT: Sorry, I originally had the last two versions the wrong way around but I've now corrected it. Just demonstrates how clear they are. |
Lately, I've noticed how python loops with range are extremely simple and powerful.
What if we expand capabilities of using go loops with range?
for v := range 25 {} // no key here can be assigned
It goes from 0 to 25.for v := range 1:25 {}
It goes from 1 to 25.The text was updated successfully, but these errors were encountered: