码迷,mamicode.com
首页 > 其他好文 > 详细

Go 消费者和生产者的简单例子

时间:2016-02-26 10:38:14      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:go channel 开发

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func production(channel chan<- string) {
    for {
        channel <- fmt.Sprintf("%v", rand.Float64())
        time.Sleep(time.Second * time.Duration(1))
    }
}

func customer(channel <-chan string) {
    for {
        message := <-channel
        fmt.Println(message)
    }
}

func main() {
    channel := make(chan string, 10)
    go production(channel)
    customer(channel)
}


本文出自 “东神要一打五” 博客,谢绝转载!

Go 消费者和生产者的简单例子

标签:go channel 开发

原文地址:http://gdutccd.blog.51cto.com/9271664/1745152

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!