你可以在写入而不是阻止时使Golang丢弃数据包吗?

Nic*_*oto 8 go

给定一个长度为N的通道,我只想在它没有满时写入它.否则我将删除此数据包并处理下一个数据包.

这可能在GOlang

cre*_*ack 18

你可以用select.例:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)