dongxing2710 2014-06-30 02:54
浏览 51
已采纳

来自fanIn的golang并发模式

I am following Rob Pike's Go concurrency pattern examples from his talk from last 2012 (the the slides are from here: http://talks.golang.org/2012/concurrency.slide#30 ).

From the example "Restoring Sequence", I keep getting an error:

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)

Here is my code

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

and my Go playground: http://play.golang.org/p/6WQE0PUF7J

What am I doing wrong?.

Sorry I'm new to Go and I would like to learn it because I want to move all of my apps and work apps from node.js to Go.

Thank you!

  • 写回答

1条回答 默认 最新

  • dszsajhd237437 2014-06-30 03:08
    关注

    The YouTube talk explains it a bit better.

    What your actual problem is, is that you're half-way through the code sample. It starts off with string channels - then moves on to Message channels.

    I have fixed this up for you. See this playground link: http://play.golang.org/p/R60AJWzr0t

    Basically, it becomes this now. Notice how all of the channels are now Message channels, not string 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
    ", <-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")
    }
    

    What your errors mean:

    prog.go:21: cannot use Message literal (type Message) as type string in send

    You're sending a Message instance on the string channel on line 21. Change it to chan Message (and all of the other channels that are string channels currently).

    prog.go:43: msg1.str undefined (type string has no field or method str)

    .. and the rest, are because you have a string channel, and each time you pop something from the channel, its a string - not a Message.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器