github.com/jwowillo/pipe@v1.2.0/example_produce_process_and_consume_test.go (about)

     1  package pipe_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jwowillo/pipe"
     7  )
     8  
     9  func Example_produceProcessAndConsume() {
    10  	p := pipe.New(
    11  		pipe.StageFunc(func(x pipe.Item) pipe.Item {
    12  			return x.(string) + "a"
    13  		}),
    14  		pipe.StageFunc(func(x pipe.Item) pipe.Item {
    15  			return x.(string) + "b"
    16  		}),
    17  		pipe.StageFunc(func(x pipe.Item) pipe.Item {
    18  			return x.(string) + "c"
    19  		}),
    20  	)
    21  	var isDone bool
    22  	pf := pipe.ProducerFunc(func() (pipe.Item, bool) {
    23  		if isDone {
    24  			return "", false
    25  		}
    26  		isDone = true
    27  		return "", true
    28  	})
    29  	cf := pipe.ConsumerFunc(func(x pipe.Item) {
    30  		fmt.Println(x)
    31  	})
    32  	pipe.ProduceProcessAndConsume(p, pf, cf)
    33  	// Output:
    34  	// abc
    35  }