Can you make Golang packages for writing instead of writing?

Given a channel of length N, I want to write to it only if it is not full. Else I will drop this package and process the next one.

Is this possible in GOlang

+8
go
source share
2 answers

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 } } } 
+18
source share

A little after I know, but this is exactly what is implemented by the OverflowingChannel type in the helper package that I wrote. It effectively uses the selected trick above.

0
source share

All Articles