请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

照着视频抄的代码,怎么还出现错误?

package main

import (
“fmt”
“sync”
)

func DoWork (id int,c chan int,wg *sync.WaitGroup) {
for n:= range c {
fmt.Printf("Worker %d received %c \n ",id,n) //从通道里取出值
}
wg.Done()
}

type worker struct {
in chan int
wg *sync.WaitGroup
}

func createWorker (id int,wg *sync.WaitGroup) worker {
w := worker {
in : make (chan int),
wg : wg,
}
go DoWork (id,w.in,wg)
return w
}

func chandemo () {

var wg sync.WaitGroup

var workers [10] worker

for i:=0;i<10;i++ {
	workers[i] = createWorker(i,&wg)  //创建通道
}

wg.Add(20)

for i,workers := range workers  {

	workers.in <- 'a' +i          //往通道里放入值

}


for i,workers := range workers  {
	workers.in <- 'A' +i
}


wg.Wait()





//time.Sleep(time.Millisecond)

}

func main() {
chandemo()
}

Worker 7 received h
Worker 8 received i
Worker 9 received j
Worker 0 received a
Worker 0 received A
Worker 1 received b
Worker 1 received B
Worker 2 received c
Worker 2 received C
Worker 3 received d
Worker 3 received D
Worker 4 received e
Worker 4 received E
Worker 5 received f
Worker 5 received F
Worker 6 received g
Worker 6 received G
Worker 9 received J
Worker 8 received I
Worker 7 received H
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc00006c004)
/usr/local/go/src/runtime/sema.go:56 +0x39
sync.(*WaitGroup).Wait(0xc00006c004)
/usr/local/go/src/sync/waitgroup.go:130 +0x64
main.chandemo()
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:56 +0x1b4
main.main()
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:69 +0x20

goroutine 18 [chan receive]:
main.DoWork(0x0, 0xc000060060, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 19 [chan receive]:
main.DoWork(0x1, 0xc0000600c0, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 20 [chan receive]:
main.DoWork(0x2, 0xc000060120, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 21 [chan receive]:
main.DoWork(0x3, 0xc000060180, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 22 [chan receive]:
main.DoWork(0x4, 0xc0000601e0, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 23 [chan receive]:
main.DoWork(0x5, 0xc000060240, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 24 [chan receive]:
main.DoWork(0x6, 0xc0000602a0, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 25 [chan receive]:
main.DoWork(0x7, 0xc000060300, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 26 [chan receive]:
main.DoWork(0x8, 0xc000060360, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

goroutine 27 [chan receive]:
main.DoWork(0x9, 0xc0000603c0, 0xc00006c004)
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:10 +0xf5
created by main.createWorker
/Users/kevin/go/src/projectalpha/channel/done/waitgroup.go:27 +0x75

Process finished with exit code 2

正在回答

1回答

这样的错误很容易查。我们把最终代码里的这段拿出来运行,看看对不对。如果对的,再通过文本比较软件来比较。照视频抄其实也很容易漏掉,还是要自己边抄边思考

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信