Golang Buffered channel
Apr 16, 2016
1 minute read

Just to put it here, to remind me why having a sending and receiving to a channel like the code below is not working.

package main

import "fmt"

func main() {
/*
this example won't run since the #10
cause the deadlock
the channel need both sender and receiver to be ready
but in this case we only create a sender #10
where receiver hasn't execute yet(#16)
we can solve this problem by using a buffered channel
this would be make(chan int,2) since we have send 2 values into the channel
*/
ch := make(chan int)
ch <- 1
ch <- 2
fmt.Println(<-ch)
fmt.Println(<-ch)
}

ref: Golang channels tutorial


Back to posts