3 ms·
go channels are like that (im a little rusty but it works pretty much like this). c := make(chan int, 2); c <- 1;//c contains 1 c <- 2;// c contains 1 follow
by lukesandberg 16y ago
go channels are like that (im a little rusty but it works pretty much like this).
c := make(chan int, 2);
c <- 1;//c contains 1
c <- 2;// c contains 1 followed by 2
y <- c;//y=1
- nitrogen 16y agoIt looks like make(chan int, 2) creates a channel with a maximum depth of two elements. I was curious how channels work, so I looked up the relevant part of the spec (http://golang.org/doc/go_spec.html#Channel_types http://golang.org/doc/go_spec.html#Channel_types). In case anyone else was wondering, specifying the length of a channel allows it to be written and read asynchronously until the channel is full. A zero-length channel will block until both a sender and receiver access the channel.