我正在嘗試解決這個 golang 練習https://github.com/loong/go-concurrency-exercises/tree/master/1-producer-consumer。我想我已經接近解決方案了,但是我遇到了死鎖錯誤davecheney tweets about golangbeertocode does not tweet about golangironzeb tweets about golangbeertocode tweets about golangvampirewalk666 tweets about golangfatal error: all goroutines are asleep - deadlock!這是我的代碼func producer(stream Stream) (tweets []*Tweet) { for { tweet, err := stream.Next() if err == ErrEOF { return tweets } tweets = append(tweets, tweet) }}func consumer(tweets []*Tweet) { for _, t := range tweets { if t.IsTalkingAboutGo() { fmt.Println(t.Username, "\ttweets about golang") } else { fmt.Println(t.Username, "\tdoes not tweet about golang") } }}func main() { start := time.Now() stream := GetMockStream() data := make(chan []*Tweet) var wg sync.WaitGroup wg.Add(3) // Producer go func() { tweets := producer(stream) data <- tweets }() // Consumer go func() { defer wg.Done() tweets := <-data consumer(tweets) }() wg.Wait() fmt.Printf("Process took %s\n", time.Since(start))}你的解決方案在哪里失?。?
- 1 回答
- 0 關注
- 92 瀏覽
添加回答
舉報
0/150
提交
取消