Concurrently printing array elements using goroutines and channels
Example
package main
import (
"fmt"
)
func main() {
var intSlice = []string{"India", "Canada", "Japan", "Australia"}
first := make(chan string)
second := make(chan string)
go odd(first)
go even(second)
for index, value := range intSlice {
if index%2 != 0 {
first <- value
} else {
second <- value
}
}
}
func odd(ch <-chan string) {
for v := range ch {
fmt.Println("ODD :", v)
}
}
func even(ch <-chan string) {
for v := range ch {
fmt.Println("EVEN:", v)
}
}
EVEN: India
EVEN: Japan
ODD : Canada
ODD : Australia
Most Helpful This Week
How to convert Boolean Type to String in Go?
How do you set cookies in an HTTP request with an HTTP client in Go?
How do you read cookies in an HTTP request with an HTTP client in Go?
Illustration of the dining philosophers problem in Golang
How append a slice to an existing slice in Golang?
How do you send an HTTP POST request with an HTTP client in Go?