Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package design
- import "errors"
- type iterator interface {
- hasNext() bool
- next() int
- }
- type bowl struct {
- cur int
- array []int
- }
- var bow bowl
- func init() {
- bow.cur = 0
- bow.array = []int{1, 2, 3}
- }
- var errEnd = errors.New("the iterator has reached the end")
- func (b *bowl) NewIterator() {
- b.cur = 0
- }
- func (b *bowl) hasNext() bool {
- return b.cur < len(b.array)
- }
- func (b *bowl) next() (int, error) {
- if b.hasNext() {
- b.cur++
- return b.array[b.cur-1], nil
- }
- return 0, errEnd
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement