{
messages := make(chan string)
messages <- "msg1"
messages <- "msg2"
fmt.Println(<-messages)
fmt.Println(<-messages)
}
In unbuffered channel there is no capacity to hold any value before it's received. In this type of channels both a sending and receiving goroutine needs to be ready at the same instant before any send or receive operation can complete
use this to run your example func main() { messages := make(chan string, 2) //buffered channel messages <- "msg1" messages <- "msg2" fmt.Println(<-messages) fmt.Println(<-messages) }
Yup i knew with buffered work, i was curious about why with unbuffered did not work
Обсуждают сегодня