pipline添加toString stage

package main

import (
    "fmt"
)

func main() {
    repeat := func(
        done <-chan interface{},
        args ...string,
    ) <-chan interface{} {
        valueStream := make(chan interface{})
        go func() {
            defer close(valueStream)
            for {
                for _, v := range args {
                    select {
                    case <-done:
                        return
                    case valueStream <- v:
                    }
                }
            }
        }()
        return valueStream
    }

    take := func(
        done <-chan interface{},
        valueStream <-chan interface{},
        num int,
    ) <-chan interface{} {
        takeStream := make(chan interface{})
        go func() {
            defer close(takeStream)
            for i := 0; i < num; i++ {
                select {
                case <-done:
                    return
                case takeStream <- <-valueStream:
                }

            }
        }()
        return takeStream
    }

    toString := func(
        done <-chan interface{},
        valueStream <-chan interface{},
    ) <-chan string {
        stringStream := make(chan string)

        go func() {
            defer close(stringStream)
            for v := range valueStream {
                select {
                case <-done:
                    return
                case stringStream <- v.(string):
                }
            }
        }()

        return stringStream
    }

    done := make(chan interface{})
    defer close(done)

    var message string
    for token := range toString(done, take(done, repeat(done, "I", "am"), 5)) {
        message += token + " "
    }

    fmt.Printf("message: [%v] \n", message)
}

程序输出如下,


pipline添加toString stage_第1张图片
image.png

你可能感兴趣的:(pipline添加toString stage)