Created
November 21, 2019 12:44
-
-
Save opethe1st/5138ec3eecb9db452383030cf74acece to your computer and use it in GitHub Desktop.
range_int that works like range in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func range_int(start int, end int, step int) func() (int, bool) { | |
current := start | |
previous := 0 | |
return func() (int, bool) { | |
if current < end { | |
previous = current | |
current += step | |
return previous, false | |
} else { | |
return 0, true | |
} | |
} | |
} | |
func main() { | |
fmt.Println("Hello, playground") | |
r := range_int(0, 10, 3) | |
for val, isEnd := r(); !isEnd; val, isEnd = r() { | |
fmt.Println(val) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment