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.