I see. There's a concrete benefit in using the waitgroup though: it would have shown you the second write blocking. https://go.dev/play/p/WNHRkkUUZUt
On Wednesday, 27 July 2022 at 10:43:01 UTC+1 aravind...@gmail.com wrote: > Thanks Brian, Got it. > "A send cannot take place until the reader is ready to receive." . So an > unbuffered channel does not have the same behaviour as a buffered channel > of size 1, fair. > > I understand part about the waitGroup, I copy pasted code used to tutor > the newcomers to programming itself. > > On Wed, Jul 27, 2022 at 1:05 PM Brian Candler <b.ca...@pobox.com> wrote: > >> BTW, using time.Sleep() to keep goroutines running is poor practice. >> >> What I suggest is using a sync.WaitGroup. Call wg.Add(1) for each >> goroutine you start; call wg.Done() when each goroutine ends; and call >> wg.Wait() to wait for them. >> https://go.dev/play/p/INl7BxG0ZSU >> >> On Wednesday, 27 July 2022 at 08:30:53 UTC+1 Brian Candler wrote: >> >>> Unless a channel is buffered, it is synchronous. A send cannot take >>> place until the reader is ready to receive. >>> >>> You can make it buffered using >>> numCh = make(chan int, 1) >>> go write() >>> go read() >>> >>> Or you can have two calls to read(): >>> >>> numCh = make(chan int) >>> go write() >>> go read() >>> go read() >>> >>> On Wednesday, 27 July 2022 at 08:13:34 UTC+1 aravind...@gmail.com wrote: >>> >>>> Hi All, >>>> When I was explaining basics of channels to newcomers, I was using the >>>> below example >>>> https://go.dev/play/p/xx2qqU2qqyp >>>> >>>> I was expecting both Write 5 and Write 3 to be printed. But only Write >>>> 5 was printed. I couldn't reason out the behaviour, can somebody point out >>>> what I am assuming wrong about. >>>> >>>> Thanks, >>>> Aravindhan K >>>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/2e036151-0069-44d9-9de2-3dcf98b33dban%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/2e036151-0069-44d9-9de2-3dcf98b33dban%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3d40de77-6432-418f-a082-df73994a739en%40googlegroups.com.