Go 消費者和生產者的簡單例子

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)
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章