我已经能够开发以下代码,该代码应该使用 go 通道将数据从一个例程传递到另一个例程:
package main import ( "fmt" "sync" ) func generateNumbers(total int, wg *sync.WaitGroup) { defer wg.Done() ch :=make(chan int) sum :=0 for idx := 1; idx <= total; idx++ { fmt.Printf("Generating number %dn", idx) sum =sum+idx ch <- sum } } func printNumbers(wg *sync.WaitGroup, ch chan int) { defer wg.Done() fmt.Printf("Sum is now",ch) for idx := 1; idx <= 3; idx++ { fmt.Printf("Printing number %dn", idx) } } func main() { var wg sync.WaitGroup ch1 :=make(chan int) wg.Add(2) go printNumbers(&wg,ch1) go generateNumbers(3, &wg) fmt.Println("Waiting for goroutines to finish...") wg.Wait() fmt.Println("Done!") }
我正在尝试将数据( Sum 的值)从generateNumbers 传递到printNumbers 例程。
但我遇到以下问题:
Waiting for goroutines to finish... Generating number 1 Sum is now%!(EXTRA chan int=0xc000102060)Printing number 1 Printing number 2 Printing number 3 fatal error: all goroutines are asleep - deadlock! goroutine 1 [semacquire]: sync.runtime_Semacquire(0xc000118270?) /usr/local/go/src/runtime/sema.go:62 +0x27 sync.(*WaitGroup).Wait(0x4ba3a8?) /usr/local/go/src/sync/waitgroup.go:116 +0x4b main.main() /tmp/tgPhZuPV77.go:42 +0x12f goroutine 19 [chan send]: main.generateNumbers(0x3, 0x0?) /tmp/tgPhZuPV77.go:19 +0xf2 created by main.main /tmp/tgPhZuPV77.go:39 +0xe5 exit status 2
请帮忙,我是 Golang 新手。
上述程序存在一些错误。
我将上面的代码修改如下:
func generateNumbers(total int, wg *sync.WaitGroup, ch chan int) { defer wg.Done() sum := 0 for idx := 1; idx <= total; idx++ { fmt.Printf("Generating number %dn", idx) sum = sum + idx ch <- sum } } func printNumbers(wg *sync.WaitGroup, ch chan int) { defer wg.Done() fmt.Printf("Sum is now:n") for idx := 1; idx <= 3; idx++ { sum := <-ch fmt.Printf("Printing number %d %dn", idx, sum) } } func main() { var wg sync.WaitGroup ch1 := make(chan int) wg.Add(2) go printNumbers(&wg, ch1) go generateNumbers(3, &wg, ch1) fmt.Println("Waiting for goroutines to finish...") wg.Wait() fmt.Println("Done!") }