你是對的,WaitGroup可能就是你想要的。但是,您沒有正確使用它。首先,您需要在調用wg.Done()的go例程之外調用wg.Add(1)。其次,調用wg.Wait()塊,直到等待組中的所有go例程完成執行,所以您不希望它同時執行。 至于short-circuiting,這并不是一個好方法。我的建議是使用上下文。在這種情況下,如果您想要真正的short-circuiting行為,您必須做的一件事是將上下文連接到nodeHasCycle的調用中。 正在修復您的代碼,我們有: func (c *cycleDet) hasCycle() bool { ctx, cancel := context.WithCancel(context.Background()) hasCycle := make(chan bool) var wg sync.WaitGroup adj := c.b // adjacency list representation of the unerlying graph for node := range adj { wg.Add(1) go func(ctx context.Context, no nodeID, co chan<- bool, cancel context.CancelFunc) { // Use wg to synchronize te