In this section, we're going to see how to pass data between Go routines. Imagine that we have two Go routines. The first Go routine performs some actions on the data and hands the data to another Go routine, which performs the second processing stage on that data. Now, we need a way to pass data between the first Go routine and the second one. As you can see, we may need some synchronization between the two Go routines because the second Go routine will have to wait until the second Go routine provides some data to it.
To begin, we are going to use the following code:
package main import "fmt" func main(){ nameChannel := make(chan string) done := make(chan string) go func(){ names := []string {"tarik", "michael", "gopi", "jessica"} for _, name := range names { // doing some operation fmt.Println("Processing the first stage of: " + name) nameChannel <- name } close(nameChannel) }() go func()...