Golang pipline的最佳实践--使用channel

栏目: Go · 发布时间: 6年前

内容简介:程序输出如下,
package main

import (
    "fmt"
)

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

    multiply := func(done <-chan interface{},
        intStream <-chan int,
        multiplier int) <-chan int {
        results := make(chan int)
        go func() {
            defer close(results)
            for i := range intStream {
                select {
                case <-done:
                    return
                case results <- i * multiplier:
                }
            }
        }()
        return results
    }

    add := func(done <-chan interface{},
        intStream <-chan int,
        additive int) <-chan int {
        results := make(chan int)
        go func() {
            defer close(results)
            for i := range intStream {
                select {
                case <-done:
                    return
                case results <- i + additive:
                }
            }
        }()
        return results
    }

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

    for v := range multiply(done, add(done, multiply(done, generator(done, 1, 2, 3, 4), 2), 1), 2) {
        fmt.Println(v)
    }
}

程序输出如下,

Golang pipline的最佳实践--使用channel

image.png


以上所述就是小编给大家介绍的《Golang pipline的最佳实践--使用channel》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

CSS

CSS

麦克法兰 / 江苏东南大学 / 2007-6 / 65.00元

《CSS(使用指南)(影印版)》主要内容:层叠样式表(CSS)能让你内心的设计思想迸发出来并得以实现。不过,将CSS与网站底层的HTML页面进行整合是一件非常困难的工作,有时甚至复杂得令人沮丧——这导致多数Web设计者不得不放弃一些奇思妙想。《CSS: The Missing Manual》可以消除Web设计工作的痛苦,并且带给你: HTML——重新入门。如果你是HTML新手,你会学到如何以......一起来看看 《CSS》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具