Channel communication behaviour with select statement in goroutines

Issue

This is a modified version of the one provided by "A Tour of Go".

package main

import "fmt"

func fibonacci(c, quit chan int) {
    x, y := 0, 1
    for {
        select {
        case c <- x:
            x, y = y, x+y
            fmt.Println("GEN", x)
        case <-quit:
            fmt.Println("quit")
            return
        }
    }
}

func main() {
    c := make(chan int)
    quit := make(chan int)
    go func() {
        for i := 0; i < 10; i++ {
            fmt.Println("DISP", <-c)
        }
        quit <- 0
    }()
    fibonacci(c, quit)
}

Here is the output for the above code:

DISP 0
GEN 1
GEN 1
DISP 1
DISP 1
GEN 2
GEN 3
DISP 2
DISP 3
GEN 5
GEN 8
DISP 5
DISP 8
GEN 13
GEN 21
DISP 13
DISP 21
GEN 34
GEN 55
DISP 34
quit

I don’t understand the behaviour of the code. Why are 2 Fibonacci numbers generated before displaying both of them? Does this depend on the execution environment?

Solution

Because the receiver in goroutine (go func() {...}) executes concurrently when receives value from sender (select statement in func fibonacci()).

By adding a delay after send a value (c <- x:) will avoid multipe sending at the same time so you can see more clearly.
Try this code: (https://play.golang.org/p/9rOdS2YThKR)

package main

import (
    "fmt"
    "time"
)

func fibonacci(c, quit chan int) {
    x, y := 0, 1
    for {
        fmt.Println("-----------------------------------------")
        fmt.Println("current x:", x)
        select {
        //Send a value into a channel using the c <- x syntax, block until receiver is ready
        case c <- x:
            //When receiver gets x value, this code will executes
            //Delay, avoid mutilple sending at the same time
            time.Sleep(5 * time.Millisecond)
            //Increase
            x, y = y, x+y
            fmt.Println("increased x to", x)
        case <-quit:
            fmt.Println("quit")
            return
        }
    }
}

func main() {
    c := make(chan int)
    quit := make(chan int)
    go func() {
        //The <-c syntax receives a value from the channel, block until sender is ready
        for i := 0; i < 10; i++ {
            fmt.Println("received x:", <-c)
        }
        quit <- 0
    }()
    fibonacci(c, quit)
}

Result for above code is:

-----------------------------------------
current x: 0
received x: 0
increased x to 1
-----------------------------------------
current x: 1
received x: 1
increased x to 1
-----------------------------------------
current x: 1
received x: 1
increased x to 2
-----------------------------------------
current x: 2
received x: 2
increased x to 3
-----------------------------------------
current x: 3
received x: 3
increased x to 5
-----------------------------------------
current x: 5
received x: 5
increased x to 8
-----------------------------------------
current x: 8
received x: 8
increased x to 13
-----------------------------------------
current x: 13
received x: 13
increased x to 21
-----------------------------------------
current x: 21
received x: 21
increased x to 34
-----------------------------------------
current x: 34
received x: 34
increased x to 55
-----------------------------------------
current x: 55
quit

Answered By – Son Huynh

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.