似乎“復雜”( getC) 函數被阻塞了。我假設通道一旦被讀取就會被銷毀,因此我想知道如何sC與getC函數和main函數共享通道而不會陷入死鎖(當前片段)package mainfunc main() {//simple function and complex function/channel sC := make(chan string) go getS(sC) cC := make(chan string) go getC(sC, cC)//collect the functions result s := <-sC//do something with `s`. We print but we may want to use it in a `func(s)`print(s)//after a while we do soemthing with `c` c := <-cC print(c)}func getS(sC chan string) { s := " simple completed " sC <- s}func getC(sC chan string, cC chan string) {//we do some complex stuff print("complex is not complicated\n")//Now we need the simple value so we try wait for the s channel. s := <-sC c := s + " more " cC <- c //send complex value}
- 3 回答
- 0 關注
- 195 瀏覽
添加回答
舉報
0/150
提交
取消