Hit deadlock when Select channel in golang

package main

import (
    "fmt"
    "time"
)

func Server1(ch chan string) {
    time.Sleep(2*time.Second)
    ch <- "from Server-1"
}

func Server2(ch chan string) {
    time.Sleep(3*time.Second)
    ch <- "from Server-2"
}

func main() {

    input1 := make(chan string)
    input2 := make(chan string)
    
    go Server1(input1)
    go Server2(input2)
    
    for {
        select {
            case s1 := <- input1:
                fmt.Printf(s1 + "\n")
            case s2 := <- input2:
                fmt.Printf(s2 + "\n")   
        }
    }
}

Ran the above code, get errors as following

from Server-1 from Server-2 fatal error: all goroutines are asleep - deadlock!

In this channel select, we have two threads running with different timer interval, Why is it a deadlock?



from Recent Questions - Stack Overflow https://ift.tt/2JafT78
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)