I was trying to create a deadlock error and was working with the following code:
package main
import "fmt"
func main() {
channel := make(chan int)
go func(channel chan int) {
for i := 0; i < 100; i++ {
channel <- i
}
}(channel)
for val := range channel {
fmt.Println(val)
}
}
When I run the program with the following command
go run -race main.go
It never returns, but the same program when I run without the race flag returns a deadlock error.
@telo_tade Please correct me if I am wrong. The deadlock in general is “waiting for something that will never happen.” In this case, the consumer will wait for value to come from the channel, but it will never happen, and there will be no other goroutines to execute, hence it will throw a deadlock error. Isn’t it?