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
--
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.