The problem is that you're sending to quit on the same goroutine that's receiving from quit quit has a buffer size of 0, which means that in order to proceed there has to be a sender on one side and a receiver on the other at the same time You're sending, but no one's on the other end, so you wait forever. In this particular case the Go runtime is able to detect the problem and panic The reason only the first value is printed when you remove quit is that your main goroutine is exiting before your remaining two are able to print Do not just increase channel buffer sizes to get rid of problems like this. It can help (although in this case it doesn't), but it only covers up the problem and doesn't truly fix the underlying cause.
Increasing a channel's buffer size is strictly an optimization. In fact, it's usually better to develop with no buffer because it makes concurrency problems more obvious There are two ways to fix the problem: Keep quit but send 0 on it in each goroutine inside ListenOnChannel In main make sure you receive a value from each goroutine before moving on. (In this case, you'll wait for three values.) Use a WaitGroup There's a good example of how it works in the documentation.
The problem is that you're sending to quit on the same goroutine that's receiving from quit. Quit has a buffer size of 0, which means that in order to proceed there has to be a sender on one side and a receiver on the other at the same time. You're sending, but no one's on the other end, so you wait forever.In this particular case the Go runtime is able to detect the problem and panic.
The reason only the first value is printed when you remove quit is that your main goroutine is exiting before your remaining two are able to print. Do not just increase channel buffer sizes to get rid of problems like this.It can help (although in this case it doesn't), but it only covers up the problem and doesn't truly fix the underlying cause. Increasing a channel's buffer size is strictly an optimization.
In fact, it's usually better to develop with no buffer because it makes concurrency problems more obvious. There are two ways to fix the problem: Keep quit, but send 0 on it in each goroutine inside ListenOnChannel. In main, make sure you receive a value from each goroutine before moving on.
(In this case, you'll wait for three values.) Use a WaitGroup. There's a good example of how it works in the documentation.
In general this looks good, but there is one problem. Remember that channels are either buffered or unbuffered (synchronous or asynchronous). When you send to an unbuffered channel or to a channel with a full buffer the sender will block until the data has been removed from the channel by a receiver.So with that, I'll ask a question or two of my own: Is the quit channel synchronous or asynchronous?
What happens in Pub when execution hits quitPub(2, quit). But there is another solution. Can you see what it is?
I don't actually get the same behavior you do if I remove.
Sorry but you got me more confused with your questions. And I can assure you that on my machine I get only one line printed if I remove quit completely. – cod3-monk-3y Nov 9 at 18:59 1 Check this out on the playground: play.golang.Org/p/42tut8OKwG The point of the questions is to try to help you figure out the problem.
The other answer is good, but I think the core issue is important here. To answer the questions: 1. Quit is synchronous because is was allocated via make(chan int).
2. This makes it synchronous meaning that the main thread will block when calling quit This behavior is just an artifact of the implementation -- it could very well change with the scheduler.As to the first question -- remember that these channels are bidirectional. What about waiting for an acknowledgement from the channel?
So you publish and wait for a response:? – robothor Nov 10 at 21:23 1 But in terms of idioms... if you are asking about when you want to choose to use a goroutine over some other construct, then I don't really have a good answer. It is always a design choice, but in general if you can think of two pieces of code as mostly independent then a concurrent approach is probably pretty reasonable.
E.g."Here, have this data, let me know when something interesting happens". It is interesting to take a look at and read Hoare's paper about Communicating Sequential Processes. – robothor Nov 10 at 21:37.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.