Concurrency 来自fanIn示例的golang并发模式

Concurrency 来自fanIn示例的golang并发模式,concurrency,go,Concurrency,Go,我下面是Rob Pike去年2012年演讲中的Go并发模式示例(幻灯片如下:) 从“恢复序列”示例中,我不断得到一个错误: prog.go:21: cannot use Message literal (type Message) as type string in send prog.go:43: msg1.str undefined (type string has no field or method str) prog.go:44: msg2.str undefined (type st

我下面是Rob Pike去年2012年演讲中的Go并发模式示例(幻灯片如下:)

从“恢复序列”示例中,我不断得到一个错误:

prog.go:21: cannot use Message literal (type Message) as type string in send
prog.go:43: msg1.str undefined (type string has no field or method str)
prog.go:44: msg2.str undefined (type string has no field or method str)
prog.go:46: msg1.wait undefined (type string has no field or method wait)
prog.go:47: msg2.wait undefined (type string has no field or method wait)
这是我的密码

type Message struct {
    str string
    wait chan bool  
}

func boring(msg string) <- chan string {
    c := make(chan string)
    waitForIt := make(chan bool)

    go func() {
        for i := 0; ; i++ {
            c <- Message{ fmt.Sprintf("%s: %d", msg, i), waitForIt }
            time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
            <-waitForIt
        }   
    }()
    return c    
}

func fanIn(input1, input2 <-chan string) <-chan string {
    c := make(chan string)
    go func() { for { c <- <-input1 } }()
    go func() { for { c <- <-input2 } }()
    return c
}

func main() {
    c := fanIn(boring("joe"), boring("ann"))

    for i := 0; i < 10; i++ {
        //fmt.Printf("You say %q\n", <-c)
        //fmt.Println(<-c)
        msg1 := <-c; fmt.Println(msg1.str)
        msg2 := <-c; fmt.Println(msg2.str)

        msg1.wait <- true
        msg2.wait <- true

        fmt.Println("--------------")
    }


    fmt.Println("Your boring, im leaving")
}
类型消息结构{
str字符串
等等,陈波
}

func无聊(msg string)YouTube上的对话解释得更好一些

您实际的问题是,代码示例已经完成了一半。它从
string
channels开始,然后转到
Message
channels

type Message struct {
    str  string
    wait chan bool
}

func boring(msg string) <-chan Message {
    c := make(chan Message)
    waitForIt := make(chan bool)

    go func() {
        for i := 0; ; i++ {
            c <- Message{fmt.Sprintf("%s: %d", msg, i), waitForIt}
            time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
            <-waitForIt
        }
    }()
    return c
}

func fanIn(input1, input2 <-chan Message) <-chan Message {
    c := make(chan Message)
    go func() {
        for {
            c <- <-input1
        }
    }()
    go func() {
        for {
            c <- <-input2
        }
    }()
    return c
}

func main() {
    c := fanIn(boring("joe"), boring("ann"))

    for i := 0; i < 10; i++ {
        //fmt.Printf("You say %q\n", <-c)
        //fmt.Println(<-c)
        msg1 := <-c
        fmt.Println(msg1.str)
        msg2 := <-c
        fmt.Println(msg2.str)

        msg1.wait <- true
        msg2.wait <- true

        fmt.Println("--------------")
    }

    fmt.Println("Your boring, im leaving")
}
我已经为你安排好了。请参阅此游乐场链接:

基本上,现在变成了这样。请注意,所有频道现在都是
消息
频道,而不是
字符串
频道

type Message struct {
    str  string
    wait chan bool
}

func boring(msg string) <-chan Message {
    c := make(chan Message)
    waitForIt := make(chan bool)

    go func() {
        for i := 0; ; i++ {
            c <- Message{fmt.Sprintf("%s: %d", msg, i), waitForIt}
            time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
            <-waitForIt
        }
    }()
    return c
}

func fanIn(input1, input2 <-chan Message) <-chan Message {
    c := make(chan Message)
    go func() {
        for {
            c <- <-input1
        }
    }()
    go func() {
        for {
            c <- <-input2
        }
    }()
    return c
}

func main() {
    c := fanIn(boring("joe"), boring("ann"))

    for i := 0; i < 10; i++ {
        //fmt.Printf("You say %q\n", <-c)
        //fmt.Println(<-c)
        msg1 := <-c
        fmt.Println(msg1.str)
        msg2 := <-c
        fmt.Println(msg2.str)

        msg1.wait <- true
        msg2.wait <- true

        fmt.Println("--------------")
    }

    fmt.Println("Your boring, im leaving")
}
类型消息结构{
str字符串
等等,陈波
}

func无聊(味精串)哦!,我怎么没注意到呢!成功了!如果它出现在youtube上,我必须再看一次,虽然我已经看了很多遍了,但我记不起他换了类型。。还是我错过了什么?我想我需要更多的学习和阅读。。我正在为我们公司建立一个聊天系统。。我想我可以很容易地在Node中编写这个,但是我已经厌倦了回调。我已经做了很多年了。。是学习新知识的时候了。。再次感谢你!没问题。我也在学习围棋,这是到目前为止最难理解的事情——假设你已经知道指针和基本的comp-sci知识。老实说,到目前为止,学习围棋已经半年了,我仍然对指针感到困惑。比如我什么时候会使用*message或&message。搞什么鬼。。在我的整个职业生涯中,我来自ruby、python和JS。这些指针都不存在。。当我可以在许多其他语言中定义或调用指针时,为什么我必须首先创建指针呢。如果你能理解它们,它将极大地帮助你理解一般的计算机。我强烈建议你仔细阅读,我会把这当作一项任务。在我读完这本关于go的书之后,我可能会读到一些关于指针和静态输入的东西。指针和静态类型对我来说很难。2) .