Hokutosei
Hokutosei

Reputation: 2164

golang concurrency pattern from fanIn example

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

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!

Upvotes: 4

Views: 1922

Answers (1)

Simon Whitehead
Simon Whitehead

Reputation: 65079

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

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.

Upvotes: 6

Related Questions