Hi All,
In advance, i’d like to thank you for taking the time to look at my code and help me understand things.
The code below is a simple execution of a buffered channel. What i’m assuming is happening here is that since the buffer in the channel has a size of 1, multiple writes to it in a short amount of time result in a buffer overflow. However, with a deadlock error message, i’m not sure if that’s the case. This is why i am confused and would like some help understanding this concept.
Here is the code:
package main
import (
"fmt"
)
func printer(reqChan chan string, resChan chan string){
for {
select {
case m := <- reqChan:
fmt.Printf("received job [%s]\n", m)
resChan <- m
}
}
}
func main(){
jobs := 5
numWorkers := 1
queSize := 1
bufChan := make(chan string, queSize)
resChan := make(chan string, queSize)
for i := 0; i < numWorkers; i++ {
go printer(bufChan, resChan)
}
// send jobs
for i := 0; i < jobs; i++ {
fmt.Println("sending job", i)
bufChan <- fmt.Sprintf("buff %d", i)
fmt.Println("sent job", i)
}
// collect responses
for i := 0; i < jobs; i++ {
fmt.Println(<- resChan)
}
}
Here is the output & error:
sending job 0
sent job 0
sending job 1
sent job 1
sending job 2
received job [buff 0]
received job [buff 1]
sent job 2
sending job 3
fatal error: all goroutines are asleep - deadlock!
thank you again!