Skip to content

Instantly share code, notes, and snippets.

@opethe1st
Created November 21, 2019 12:44
Show Gist options
  • Save opethe1st/5138ec3eecb9db452383030cf74acece to your computer and use it in GitHub Desktop.
Save opethe1st/5138ec3eecb9db452383030cf74acece to your computer and use it in GitHub Desktop.
range_int that works like range in Python
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