比较两个单独goroutine中的两个切片并使用sync.Waitgroup时挂起goroutines

比较两个单独goroutine中的两个切片并使用sync.Waitgroup时挂起goroutines,go,concurrency,channel,goroutine,Go,Concurrency,Channel,Goroutine,我在学习goroutines,在两个goroutines中比较两个切片,这是一个无限循环,永远比较它,这可能不是最好的例子,我不明白为什么挂起它 for ;; { var wg sync.WaitGroup wg.Add(2) go FindinFirst(&Arr1, &Arr2, AddChan, &wg) go FindinSecond(&Arr2, &Arr1, DelChan, &wg) count

我在学习goroutines,在两个goroutines中比较两个切片,这是一个无限循环,永远比较它,这可能不是最好的例子,我不明白为什么挂起它

for ;; {
    var wg sync.WaitGroup
    wg.Add(2)
    go FindinFirst(&Arr1, &Arr2, AddChan, &wg)
    go FindinSecond(&Arr2, &Arr1, DelChan, &wg)
    counter := 0
        for ; ; {
            select {
            case Add, ok := <- AddChan:
                if ok == true {
                    for k, v := range Add  {
                        fmt.Println(k, ":", v)
                    }
                }else {
                    counter += 1
                }
            case Del, ok := <- DelChan:
                if ok == true {
                    for k, v := range Del  {
                        fmt.Println(k, ":", v)
                    }
                }else {
                    counter += 1
                    }
            }
            if counter == 2{
                break
            }
            wg.Wait()
}
for;;{
var wg sync.WaitGroup
工作组.添加(2)
先查找(&Arr1和&Arr2,AddChan和wg)
第二次搜索(&Arr2,&Arr1,DelChan,&wg)
计数器:=0
为了{
挑选{

case Add,ok:=我不认为在这里使用等待组有任何用处……因为您正在for循环中使用通道,所以放置
wg.wait()
将阻塞,直到所有等待组完成

在这种情况下,将一个值放入AddChan将被阻止,除非有人在等待它。代码只在第一个情况下工作,然后挂起

您可以删除
wg.wait()
和外部for循环,它就会工作

func main() {
    Arr1 := []string{"a", "b", "c", "d"}
    Arr2 := []string{"c", "e", "f", "g"}
    AddChan := make(chan string)
    DelChan := make(chan string)

    var wg sync.WaitGroup
    wg.Add(2)
    go FindinFirst(&Arr1, &Arr2, AddChan, &wg)
    go FindinSecond(&Arr2, &Arr1, DelChan, &wg)
    counter := 0
    for {
        select {
        case Add, ok := <-AddChan:
            if ok == true {
               fmt.Println(Add)
            } else {
                counter += 1
            }
        case Del, ok := <-DelChan:
            if ok == true {
                fmt.Println(Del)
            } else {
                counter += 1
            }
        }

        //if both the channels are closed, we are good, hence exit
        if counter == 2 {
            break
        }
    }
}
func main(){
Arr1:=[]字符串{“a”、“b”、“c”、“d”}
Arr2:=[]字符串{“c”、“e”、“f”、“g”}
AddChan:=make(chan字符串)
DelChan:=make(chan字符串)
var wg sync.WaitGroup
工作组.添加(2)
先查找(&Arr1和&Arr2,AddChan和wg)
第二次搜索(&Arr2,&Arr1,DelChan,&wg)
计数器:=0
为了{
挑选{

case Add,ok:=我不认为在这里使用等待组有任何用处……因为您正在for循环中使用通道,所以放置
wg.wait()
将阻塞,直到所有等待组完成

在这种情况下,将一个值放入AddChan将被阻止,除非有人在等待它。代码只在第一个情况下工作,然后挂起

您可以删除
wg.wait()
和外部for循环,它就会工作

func main() {
    Arr1 := []string{"a", "b", "c", "d"}
    Arr2 := []string{"c", "e", "f", "g"}
    AddChan := make(chan string)
    DelChan := make(chan string)

    var wg sync.WaitGroup
    wg.Add(2)
    go FindinFirst(&Arr1, &Arr2, AddChan, &wg)
    go FindinSecond(&Arr2, &Arr1, DelChan, &wg)
    counter := 0
    for {
        select {
        case Add, ok := <-AddChan:
            if ok == true {
               fmt.Println(Add)
            } else {
                counter += 1
            }
        case Del, ok := <-DelChan:
            if ok == true {
                fmt.Println(Del)
            } else {
                counter += 1
            }
        }

        //if both the channels are closed, we are good, hence exit
        if counter == 2 {
            break
        }
    }
}
func main(){
Arr1:=[]字符串{“a”、“b”、“c”、“d”}
Arr2:=[]字符串{“c”、“e”、“f”、“g”}
AddChan:=make(chan字符串)
DelChan:=make(chan字符串)
var wg sync.WaitGroup
工作组.添加(2)
先查找(&Arr1和&Arr2,AddChan和wg)
第二次搜索(&Arr2,&Arr1,DelChan,&wg)
计数器:=0
为了{
挑选{
案例添加,确定:=
func main() {
    Arr1 := []string{"a", "b", "c", "d"}
    Arr2 := []string{"c", "e", "f", "g"}
    AddChan := make(chan string)
    DelChan := make(chan string)

    var wg sync.WaitGroup
    wg.Add(2)
    go FindinFirst(&Arr1, &Arr2, AddChan, &wg)
    go FindinSecond(&Arr2, &Arr1, DelChan, &wg)
    counter := 0
    for {
        select {
        case Add, ok := <-AddChan:
            if ok == true {
               fmt.Println(Add)
            } else {
                counter += 1
            }
        case Del, ok := <-DelChan:
            if ok == true {
                fmt.Println(Del)
            } else {
                counter += 1
            }
        }

        //if both the channels are closed, we are good, hence exit
        if counter == 2 {
            break
        }
    }
}