Answer the question
In order to leave comments, you need to log in
Special magic with channels in golang?
Take a look here gentlemen.
I have 30 goroutines competing to be the first to send their number to the channel.
The judge goroutine takes the first number from the channel and closes it, because he did his task, and we are not interested in the numbers from the losing gorutins.
And now the crux of the matter.
First, why can't I make the channel buffered (with a buffer size of 1)? For example, with the goal of immediately launching something important in the winning routine, and not waiting until the referee goroutine deigns to pull out the number?
Secondly, what happens if you close the buffered channel before reading from it all the values that have managed to get there? Will such a chan be collected by the garbage collector when it goes out of scope? What will happen to the objects inside the channel?
Let there be dispute.
Answer the question
In order to leave comments, you need to log in
Let me answer the second question first.
close() has nothing to do with scope at all, it just makes the channel "closed", that is, such that when reading values from the channel and writing from it, a panic occurs. It's all. Respectively...
what happens if you close the buffered channel before reading from it all the values that have managed to get there?The channel will close, the next time you try to read a value from it, you will get a panic. The values that remained inside, consider lost, you will never get them again.
Will such a chan be collected by the garbage collector when it goes out of scope? What will happen to the objects inside the channel?It will be collected by the garbage collector only when no one else refers to it (if it is declared locally, then yes, out of scope). Objects inside will also be collected if no one else refers to them. Those that are still referenced by someone will remain.
select {
case b <- number:
fmt.Printf("Sent into b: %d\n", number)
default:
fmt.Printf("b chan closed\n")
}
This piece is very misinforming. Firstly, select to write with a default section in no way saves from panic when writing to a private channel. It just makes writing to the channel always non-blocking. As soon as you try to write something into a closed channel with such a select, you will immediately panic. Therefore, correctly for perception, this place looks like this:select {
case b <- number:
fmt.Printf("Sent into b: %d\n", number)
default:
fmt.Printf("Number %d just has been thrown away\n", number)
}
If you make channel a buffered, then your panics will fly to you, because you are writing to a closed channel. The judge goroutine takes the first number from the channel and closes it, because he did his task, and we are not interested in the numbers from the losing gorutins.It is ideal to close the channel to the sending side.
And now the crux of the matter.Firstly. You can.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question