create a buffered int channel in go

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.go
package main

import "fmt"

func main() {
    // create a buffered int channel with capacity for 2 integers
    ch := make(chan int, 2)

    // send two integers to the channel
    ch <- 1
    ch <- 2

    // receive and print the integers from the channel
    fmt.Println(<-ch)
    fmt.Println(<-ch)
}
303 chars
17 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