You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
4242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242
Infinite loop
Program exited.
From my understanding, it seems that Value.Recv() doesn't behave the same as the <- operator when it is in the condition statement of a for loop - not popping the front element from the queue.
However, since the original code passes the test, I tweaked the code a little and printed the values:
So in the conclusion, the behavior of value.Recv is hardly expectable(Moreover, if I ignore the ok variable in the body as v, _ = val.Recv(), I get the infinite loop again). Perhaps this is why the original author wrote the code like this.
Nevertheless, personally I think this line easily confuses newbies like me. It is neither using the familiar <- operator nor val := range channel. Furthermore, it gives the impression that we should poll the channel twice to get the value we want.
If it is okay, I would like to make a simple fix on this part as a PR.
The text was updated successfully, but these errors were encountered:
UponTheSky
changed the title
The chan part in the Reflection chapter is not working as expected.
The code of chan part in the Reflection chapter is a little bit unintuitive
Feb 11, 2024
Hi, it is a great pleasure to go through this wonderful content. I am learning a lot from this.
While I was reading and testing the
chan
part in the chapter Reflection, the loop part was not much intuitive to me.So I tried this code instead(at first, I thought it might be a typo):
However, the test didn't seem to be running. So mimicking the code in the page(the docs doesn't say much about
Value.Recv
), and I've got this result:From my understanding, it seems that
Value.Recv()
doesn't behave the same as the<-
operator when it is in the condition statement of afor
loop - not popping the front element from the queue.However, since the original code passes the test, I tweaked the code a little and printed the values:
So in the conclusion, the behavior of
value.Recv
is hardly expectable(Moreover, if I ignore theok
variable in the body asv, _ = val.Recv()
, I get the infinite loop again). Perhaps this is why the original author wrote the code like this.Nevertheless, personally I think this line easily confuses newbies like me. It is neither using the familiar
<-
operator norval := range channel
. Furthermore, it gives the impression that we should poll the channel twice to get the value we want.If it is okay, I would like to make a simple fix on this part as a PR.
The text was updated successfully, but these errors were encountered: