couldn't print numbers ordered with goroutine

177 views
Skip to first unread message

Taňryberdi Şyhmyradow

unread,
Apr 20, 2024, 7:18:24 PM4/20/24
to golan...@googlegroups.com, golan...@googlegroups.com
Hello guys,
For the following lines, I wanted to print numbers in ordered, but couldn't. Could you please help me and explain the reason
Thanks in advance

```
numbers := []int{1, 2, 3, 4, 5}

// Create a buffered channel to handle multiple values
printed := make(chan int, len(numbers))

for _, n := range numbers {
fmt.Println("Sending", n, "to the channel")
go func() {
printed <- n
}() // Pass the value of n by copying it
}

// Receive all values from the channel in a loop
for i := 0; i < len(numbers); i++ {
fmt.Println(<-printed)
}
```


--
Tanryberdi Shyhmyradov

Justin Israel

unread,
Apr 20, 2024, 8:18:18 PM4/20/24
to golang-nuts
When you start a bunch of goroutines in a loop, there is no guarantee as to what order the scheduler will start each one. 
Thus you will see them delivered in different orders on each run. You have to decide on some form of synchronization. Maybe you choose to run a single goroutine worker that will loop over the source slice, and push the values into the channel in order. Or maybe you will keep using many goroutines but collect them all in the receiver, sort them after the last value is received, and then print them out. Or, maybe your receiver will have some kind of buffering where it collects values and only prints them when it has the next one in sequence. 


--
Tanryberdi Shyhmyradov

Robert Solomon

unread,
Apr 20, 2024, 9:55:00 PM4/20/24
to golang-nuts
channels are not queues, as Justin said

Dan Kortschak

unread,
Apr 20, 2024, 10:07:24 PM4/20/24
to golan...@googlegroups.com
On Sat, 2024-04-20 at 18:55 -0700, Robert Solomon wrote:
> channels are not queues, as Justin said

They can be; buffered channels are queues.

From https://go.dev/ref/spec#Channel_types

> Channels act as first-in-first-out queues. For example, if one
> goroutine sends values on a channel and a second goroutine receives
> them, the values are received in the order sent.

Justin Israel

unread,
Apr 20, 2024, 11:07:34 PM4/20/24
to Dan Kortschak, golang-nuts
And really I wasn't even commenting on the nature of the channel. Only the scheduling of the goroutines. Buffered or not, they would still be random order right? 



--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/oMFIFDi_Irg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/05501e4c795fdcb4b91ffa3c35f95984772c9cde.camel%40kortschak.io.

Dan Kortschak

unread,
Apr 20, 2024, 11:40:49 PM4/20/24
to golang-nuts
On Sun, 2024-04-21 at 15:06 +1200, Justin Israel wrote:
> And really I wasn't even commenting on the nature of the channel.
> Only the scheduling of the goroutines. Buffered or not, they would
> still be random order right? 

Absolutely. Your answer was spot on. The issue is the ordering of the
goroutines' execution, not the behaviour of channels. I just wanted to
clarify the comment that responsed to that.

Reply all
Reply to author
Forward
0 new messages