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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With