阿晨1998
var Z = "Z"func Loop() { sc := make(chan *string) ss := make([]string, 0) done := make(chan struct{}, 1) go func() { //1 QUERY slice1 := []string{"a", "b", "c"} //2 WG INIT var wg1 sync.WaitGroup wg1.Add(len(slice1)) //3 LOOP-> loopSlice1(slice1, sc, &wg1) //7 WG WAIT<- wg1.Wait() sc <- &Z done <- struct{}{} }() go func() { var cc *string for { cc = <-sc log.Infof("<-sc %s", *cc) if *cc == Z { break } ss = append(ss, *cc) } }() <-done log.Infof("FUN: %#v", ss)}func loopSlice1(slice1 []string, sc chan *string, wg1 *sync.WaitGroup) { for i, x := range slice1 { //4 GO go func(n int, v string) { //5 WG DONE defer wg1.Done() //6 DOING //[1 QUERY slice2 := []string{"X", "Y", "Z"} //[2 WG INIT var wg2 sync.WaitGroup wg2.Add(len(slice2)) //[3 LOOP -> loopSlice2(n, v, slice2, sc, &wg2) //[7 WG WAIT <- wg2.Wait() }(i, x) }}func loopSlice2(n1 int, v1 string, slice2 []string, sc chan *string, wg2 *sync.WaitGroup) { for j, y := range slice2 { //[4 GO go func(n2 int, v2 string) { //[5 WG DONE defer wg2.Done() //[6 DOING r := fmt.Sprintf("%v%v %v,%v", n1, n2, v1, v2) sc <- &r }(j, y) }}