Thanks. I want the receiver always get the relately new vaule, I don't
want the sender blocked and I either choose drop the current value or the
first value of the channel. But I don't find a way to safely drop the first
value from the channel.
Maybe like this ?
```
// c is buffered channel
select {
case c <- result:
default:
// full, drop old one
go func() {
<-c
c <- result
}()
}
```
在2020年11月13日星期五 UTC+8 上午11:27:23<Kurtis Rader> 写道:
> On Thu, Nov 12, 2020 at 6:19 PM 陶青云 <[email protected]> wrote:
>
>> ```
>>
> // c is buffered channel
>> select {
>> case c <- result:
>> default:
>> // full, drop old one
>> <-c
>> c <- result
>> }
>> ```
>>
>> I have a code like this, but there may be a race condition in the
>> default case. I enconter a
>> deadlock that the goroutine block on 'c<'.
>>
>
> In addition to what Ian said you should note that a buffered channel is
> essentially a FIFO queue. The equivalent code that operates on a queue in
> any other language (e.g., Python) will have the same race. What you're
> asking for is something like the behavior provided by an API such as
> Python's collections.deque
> <https://docs.python.org/2/library/collections.html#collections.deque>.
> Whether any of the solutions you might find by searching for something like
> "golang deque" satisfy your requirements can't be answered given the
> information you have provided.
>
> --
> Kurtis Rader
> Caretaker of the exceptional canines Junior and Hank
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/af647672-b03e-4b1f-a688-249bfd316f07n%40googlegroups.com.