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 append struct member dynamically using Empty Interface?
How to convert Struct fields into Map String?
How to check specific field exist in struct?
How to initialize a struct containing a slice of structs in Golang?
How to build a map of struct and append values to it?
Golang panic recover example