【问题标题】:golang concurrency pattern from fanIn example来自 fanIn 示例的 golang 并发模式
【发布时间】:2014-07-02 02:39:06
【问题描述】:

我正在关注 Rob Pike 在 2012 年的演讲中的 Go 并发模式示例(幻灯片来自这里:http://talks.golang.org/2012/concurrency.slide#30)。

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

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

还有我的 Go 操场:http://play.golang.org/p/6WQE0PUF7J

我做错了什么?

抱歉,我是 Go 新手,我想学习它,因为我想将我的所有应用程序和工作应用程序从 node.js 移动到 Go。

谢谢!

【问题讨论】:

标签: concurrency go


【解决方案1】:

YouTube 谈话解释得更好。

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

我已经为你解决了这个问题。查看此游乐场链接:http://play.golang.org/p/R60AJWzr0t

基本上,它现在变成了这个。注意现在所有频道都是Message 频道,而不是string 频道。

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

你的错误是什么意思:

prog.go:21: 不能在发送中使用消息文字(消息类型)作为类型字符串

您将在第 21 行的 string 频道上发送 Message 实例。将其更改为 chan Message(以及当前所有其他字符串频道)。

prog.go:43: msg1.str 未定义(字符串类型没有字段或方法 str)

.. 其余的,是因为你有一个 string 频道,每次你从频道弹出一些东西时,它都是 string - 而不是 Message

【讨论】:

  • 哦!我怎么没注意到!有效!如果它在 youtube 上,我必须再看一遍,虽然我已经看过很多遍了,我不记得他改变了类型..还是我错过了什么?我想我需要学习和阅读更多内容。我正在为我们公司构建一个聊天系统。我想我可以很容易地用 Node 编写这个,但我已经厌倦了回调。多年来一直这样做..是时候学习新东西了..再次感谢您!
  • 没问题。我也在学习 Go,这是迄今为止最难理解的事情——假设你已经知道指针和基本的 comp sci 东西。
  • 老实说,到现在为止,已经学了半年的 GO 了,对指针还是一头雾水。比如我什么时候使用 *message 或 &message。什么鬼……在我的整个职业生涯中,我来自 ruby​​、python 和 JS。这些指针都不存在..为什么我必须首先创建一个指针,而我可以动态定义或调用它
  • 指针存在于许多其他语言中。如果您理解它们,它将极大地帮助您理解计算机。我强烈建议您阅读它们。
  • 我会把它当作一项任务。在我读完这本关于 Go 的书后,我可能会读到一些关于指针和静态类型处理的东西。指针和静态类型这两个对我来说很难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多