Why is deadlock possible here?

Hi. I am currently learning about deadlocks in Go and when they can appear. I am viewing this code. I know a deadlock is possible here, but I cannot find it. Can someone please explain it to me?

package main

import "fmt"

func reuters(ch chan string) {
	ch <- "REUTERS"
}
func bloomberg(ch chan string) {
	ch <- "BLOOMBERG"
}
func newsReader(reutersCh chan string, bloombergCh chan string) {
	ch := make(chan string, 5)
	go func() {
		ch <- (<-reutersCh)
	}()
	go func() {
		ch <- (<-bloombergCh)
	}()
	x := <-ch
	fmt.Printf("got news from %s \n", x)
}
func main() {
	reutersCh := make(chan string)
	bloombergCh := make(chan string)
	go reuters(reutersCh)
	go bloomberg(bloombergCh)
	newsReader(reutersCh, bloombergCh)
	newsReader(reutersCh, bloombergCh)
}

The first call to newsReader causes both values to be read, so there is nothing to read during the second call.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.