首页 > 其他 > 详细

使用channel实现goroutine

时间:2018-12-24 22:33:53      阅读:165      评论:0      收藏:0      [点我收藏+]
使用channel实现goroutine
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")

}

使用channel实现goroutine

原文:http://blog.51cto.com/huwho/2334813

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!