go实现生产者消费者

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    ch := make(chan int)
    done := make(chan bool)
    f := make(chan bool)
    go func() {
        for {
            select {
            case ch <- rand.Intn(5):{
                fmt.Println("随机生成数字")
            }
            case <-f:
                {
                    fmt.Println(111)
                    return
                }
            default:
            }
        }
    }()

    go func() {
        for i := 0; i < 5; i++ {
            fmt.Println("Rand Number = ", <-ch) // Print number received on standard output
        }
        f <- true
        done <- true // Send Terminate Signal and return
        return
    }()
    <-done // Exit Main when Terminate Signal received
}
原文地址:https://www.cnblogs.com/wujf/p/10881929.html