Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list all elements in a buffered channel

Tags:

go

channel

Is there a (non destructive) way to list all the element in a buffered channel?

The only thing I can think about is to cycle all of them, reinserting them at the end. This doesn't seem the smartest approach.

Link to playground

c := make(chan int, 100)
c <- 111
c <- 222
for i:=0;i<2;i++ {
element := <- c
fmt.Println(element)
c <- element
}
fmt.Println(len(c))
like image 332
meto Avatar asked Sep 25 '14 18:09

meto


2 Answers

No you can't, you can write your own blocking queue based on a list if you want to do that.

like image 185
OneOfOne Avatar answered Nov 15 '22 08:11

OneOfOne


This thread from 2011 offered some wrapper around a channel in order to enable a Peek() function, but that was more a workaround than anything else.

type PeekChanInt struct {
        in <-chan int
        out chan int
}

The general conclusion was:

A synchronous channel has no head (it's like a zero-length slice)

You can't do that because

  • a) it gets put back at the tail of the queue rather than the end and
  • b) a writer may have got there first, so the put may block.

I have wanted a feature like this in the past. It can make sense when there is only one consumer of the channel (I wanted it to peek at the first mouse event to do hit testing before deciding whether to consume it)

You can simulate it with a process acting as intermediary, but you'd have to do it for each channel type or lose type safety.

Remember there is no buffering, so if you peek at a value, you'd have to get the value from the other side, which would be equivalent to reading it - but that's wrong because peeking should have no side effects.

like image 25
VonC Avatar answered Nov 15 '22 07:11

VonC