Channel中的一个数据由两个例程接收

时间:2016-10-30 03:11:07

标签: go channel goroutine

您好我了解了常规和渠道。 我做了一些频道实验,我通过频道发送数据并尝试用2个函数捕获它。但我的第二个功能没有运行

这是我的代码:

package main

import (
    "fmt"
    "os"
    "time"
)

func timeout(duration int, ch chan<- bool) {
    time.AfterFunc(time.Duration(duration)*time.Second, func() {
        ch <- true
    })
}

func watcher(duration int, ch <-chan bool) {
    <-ch
    fmt.Println("\nTimeout! no Answer after", duration, "seconds")
    os.Exit(0)
}

func watcher2(duration int, ch <-chan bool) {
    <-ch
    fmt.Println("This is watcher 2 as a second receiver")
}

func main() {
    var data = make(chan bool)
    var duration = 5

    go timeout(duration, data)
    go watcher(duration, data)
    go watcher2(duration, data)

    var input string
    fmt.Print("What is 725/25 ? ")
    fmt.Scan(&input)

    if input == "29" {
        fmt.Println("Correct")
    } else {
        fmt.Println("Wrong!")
    }
}

你能告诉我一些关于它的解释吗? 谢谢

2 个答案:

答案 0 :(得分:1)

正如@Andy Schweig所说,你只能从Go频道拉一次。如果您仍希望接收消息两次,则可以使用Observer设计模式:

import "fmt"

type Observer interface {
    Notify(message string)
}

type Watcher struct {
    name string
}

func (w Watcher) Notify(message string) {
    fmt.Printf("Watcher %s got message %s\n", w.name, message)
}

var watchers =  [...]Watcher {{name: "Watcher 1"}, {name: "Watcher 2"}}
var c = make(chan string)

func notifier() {

    var message string
    for {
        // Messaged pulled only once
        message = <- c

        // But all watchers still receive it
        for _, w := range watchers {
            w.Notify(message)
        }
    }
}

func main() {
    go notifier()

    c <- "hello"
    c <- "how are you?"
}

答案 1 :(得分:1)

您宣布的channel只能处理一个接收者。默认情况下,channelsunbuffered,这意味着如果有相应的接收方接收发送的值,它们将只接受发送。而buffered通道接受有限数量的值而没有相应的接收器用于这些值。如果您要注入多个输入及其后续接收,则需要将channel声明为buffered channel

ch := make(chan bool, n) //n being the number of items to buffer