您能否在写入而不是阻止时使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
        }
    }
}

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