成都网站建设设计

将想法与焦点和您一起共享

使用channel实现goroutine

使用channel实现goroutine

1.无缓冲channel

成都创新互联服务项目包括龙泉网站建设、龙泉网站制作、龙泉网页制作以及龙泉网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,龙泉网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到龙泉省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!



    package main

    import (
        "fmt"
        "time"
    )

    var message = make(chan string)

    //往channel中输入信息
    func sample1()  {
        message <- "hello gorotine."
    }

    //消费channel中的信息
    func sample2()  {
        str := <- message
        str = str + " run fast,run the world."
        message <- str

    }

    func main()  {
        go sample1()
        go sample2()
        time.Sleep(time.Second)
        fmt.Println(<-message)
        fmt.Println("message")

    }

2.buffer channel(缓冲channel)



    package main

    import (
        "fmt"
        "time"
    )

    /*
        1.buffer channel
        2.在函数中传递channel,而不是申明一个全局变量channel
        3.FIFO:first input first output
     */

    //往channel中输入信息
    func sample1(message chan string)  {
        message <- "hello gorotine.1"
        message <- "hello gorotine.2"
        message <- "hello gorotine.3"
        message <- "hello gorotine.4"
    }

    //消费channel中的信息
    func sample2(message chan  string)  {
        str := <- message
        str = str + " run fast,run the world."
        message <- str

    }

    func main()  {
        var message = make(chan string, 4)
        go sample1(message)
        go sample2(message)
        time.Sleep(time.Second)
        fmt.Println(<-message)
        fmt.Println(<-message)
        fmt.Println(<-message)
        //fmt.Println(<-message)

        fmt.Println("message")

    }
   // 2.1 output1
    hello gorotine.2
    hello gorotine.3
    hello gorotine.4
    message

取消这行代码的注释//fmt.Println(<-message),将会得到如下的输出,这正好说明了先进先出的概念

  //2.2 output2
    hello gorotine.2
    hello gorotine.3
    hello gorotine.4
    hello gorotine.1 run fast,run the world.
    message

3.遍历channel及关闭遍历channel

package main

import (
    "fmt"
    "time"
)

/*
    1.buffer channel
    2.在函数中传递channel,而不是申明一个全局变量channel
    3.FIFO:first input first output
 */

//往channel中输入信息
func sample1(message chan string)  {
    message <- "hello gorotine.1"
    message <- "hello gorotine.2"
    message <- "hello gorotine.3"
    message <- "hello gorotine.4"
}

//消费channel中的信息
func sample2(message chan  string)  {
    str := <- message
    str = str + " run fast,run the world."
    message <- str

}

func main()  {
    var message = make(chan string, 4)
    go sample1(message)
    go sample2(message)
    time.Sleep(time.Second)
    //使用range遍历channel
    for str := range message {
        fmt.Println(str)
    }

    fmt.Println("message")

}

不关闭channel,那么channel默认为空;那此时如果遍历,则会抛出异常

//3.1output1
hello gorotine.2
hello gorotine.3
hello gorotine.4
hello gorotine.1 run fast,run the world.
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
    D:/run/pms/src/test.go:37 +0x126

下面演示关闭channel的遍历

package main

import (
    "fmt"
    "time"
)

/*
    1.buffer channel
    2.在函数中传递channel,而不是申明一个全局变量channel
    3.FIFO:first input first output
 */

//往channel中输入信息
func sample1(message chan string)  {
    message <- "hello gorotine.1"
    message <- "hello gorotine.2"
    message <- "hello gorotine.3"
    message <- "hello gorotine.4"
}

//消费channel中的信息
func sample2(message chan  string)  {
    str := <- message
    str = str + " run fast,run the world."
    message <- str
    close(message)

}

func main()  {
    var message = make(chan string, 4)
    go sample1(message)
    go sample2(message)
    time.Sleep(time.Second)
    //使用range遍历channel
    for str := range message {
        fmt.Println(str)
    }

    fmt.Println("message")

}
//3.2output2
hello gorotine.2
hello gorotine.3
hello gorotine.4
hello gorotine.1 run fast,run the world.
message

名称栏目:使用channel实现goroutine
文章地址:http://chengdu.cdxwcx.cn/article/gsiidd.html