To create a buffered int channel in golang, we can use the built-in make
function with the chan
keyword to declare and initialize a new channel with a specified capacity.
Here's an example of how to create a buffered int channel with capacity for two integers:
main.go303 chars17 lines
In this example, we create a buffered int channel ch
with a capacity of 2 using make(chan int, 2)
. We then send two integers to the channel (ch <- 1
and ch <- 2
). Finally, we receive and print the integers from the channel (fmt.Println(<-ch)
). Since the channel is buffered, it can hold up to two integers, and we can receive them in any order.
gistlibby LogSnag