Given a channel of length N, I want to write to it only if it is not full. Else I will drop this packet and process the next one.
Is this possible in GOlang
Given a channel of length N, I want to write to it only if it is not full. Else I will drop this packet and process the next one.
Is this possible in GOlang
You can use select
. Example:
package main
func main() {
ch := make(chan int, 2)
for i := 0; i < 10; i++ {
select {
case ch <- i:
// process this packet
println(i)
default:
println("full")
// skip the packet and continue
}
}
}