It works correctly. What you see is the race between the printf calls at each iteration
On Thu, Jan 24, 2019 at 2:40 PM diego patricio <dipas...@gmail.com> wrote: > Hi, I just want to achieve that one goroutine send a data and another > goroutine receive it and print it consequently, for instance o, 1, 2, 3. > > Is there a correct way to achieve this ? > > Regards > > El jue., 24 ene. 2019 a las 23:08, Ian Denhardt (<i...@zenhack.net>) > escribió: > >> In general, after the message send goes through, the two goroutines >> involved could start executing again in any order -- so the behavior >> you're seeing is expected? >> >> Is there a less toy example that this is supposed to be representative >> of? Or were you just expecting it to work differently, and trying to >> understand the behavior? >> >> It's hard to give advice without knowing what you're trying to achieve; >> If I wanted exactly the output you're going for I'd just do: >> >> for i := 0; i < 10; i++ { >> fmt.Printf("Producer %v\n", i) >> fmt.Printf("Consumer %v\n", i) >> } >> >> ..but I imagine that's not the point of the excercise. >> >> -Ian >> >> Quoting diego patricio (2019-01-24 16:48:35) >> > Hi, thanks for your response, still� I dont get right result >> > image.png >> > image.png >> > >> > El jue., 24 ene. 2019 a las 22:42, Christian Staffa >> > (<[1]christian.p.sta...@gmail.com>) escribió: >> > >> > Hi >> > to get a synchronization with goroutines you need to use an >> unbuffered >> > channel. you have used a buffered channel of 1. >> > Sent from my iPhone >> > On 24. Jan 2019, at 22:11, diego patricio <[2]dipas...@gmail.com> >> > wrote: >> > >> > Hi all, i'am just learning Go and goroutines, I have three goroutines >> > (main, producer, consumer) and I dont know how synchronize producer >> and >> > consumer for print one value at time, the output that I want is >> > Producer 0 >> > Consumer 0 >> > Producer 1 >> > Consumer 1 >> > ...... >> > but the output of my program it's diferent. >> > the program: >> > <image.png> >> > The output: >> > <image.png> >> > Sorry about my english >> > Regards >> > >> > -- >> > 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 [3]golang-nuts+unsubscr...@googlegroups.com. >> > For more options, visit [4]https://groups.google.com/d/optout. >> > >> > -- >> > 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 [5]golang-nuts+unsubscr...@googlegroups.com. >> > For more options, visit [6]https://groups.google.com/d/optout. >> > >> > Verweise >> > >> > 1. mailto:christian.p.sta...@gmail.com >> > 2. mailto:dipas...@gmail.com >> > 3. mailto:golang-nuts+unsubscr...@googlegroups.com >> > 4. https://groups.google.com/d/optout >> > 5. mailto:golang-nuts+unsubscr...@googlegroups.com >> > 6. https://groups.google.com/d/optout >> > -- > 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. > For more options, visit https://groups.google.com/d/optout. > -- *Michael T. jonesmichael.jo...@gmail.com <michael.jo...@gmail.com>* -- 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. For more options, visit https://groups.google.com/d/optout.