go - How to print contents of channel without changing it -


i'm writing program in go language, , have simple problem:

i have goroutines in program , channels goroutines use communicate. time time check inside channels. how achieve without interrupting goroutines' work? channels have function print contents? or should somehow copy them?

var shelf chan int = make(chan int, 5) go depot(shelf) go shop(shelf) var input string fmt.scanln(&input) if (input == "print") {      //here print on shelf } 

how achieve without interrupting goroutines' work?

the simple answer can't, without interrupting. channels synchronization primitive, meaning enables concurrent programs communicate safely. if take out of channel, "taking out" happens atomically, nobody else can take same item out of same channel. , that's intended.

what can take items out , put them after printing them. problem approach elements might never printed while others may printed more once goroutines involved race grab items channel.

it sounds need else channel.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -