Standard library / iter

iter.Seq

A push iterator yielding one value at a time. The iterator must stop yielding when yield returns false. May be reusable or documented as single-use. Expected stdout: '1\n2\n'.

Formal Syntax & Structure

go syntaxgo
type Seq[V any] func(yield func(V) bool)

Working Example

Example Snippetgo
package main
import (  "fmt"  "iter")
func main() {  var seq iter.Seq[int] = func(yield func(int) bool) {    for i := 1; i <= 3; i++ {      if !yield(i) {        return      }    }  }  for v := range seq {    fmt.Println(v)    if v == 2 {      break    }  }}
Go Reference