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