goroutine 被挂起时传递数据
时间:2024-02-17
浏览:267
作者:佚名
在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《goroutine 被挂起时传递数据》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!
问题内容
给定以下变量
wg.add(4) pdayresch := make(chan map[string]map[string]int) pdayerrch := make(chan error)
以下代码挂起
// convert the previous, current, and next day prayers and put them in map
go func() {
fmt.println("executing first go routine") // todo: remove this line after debug
data, err := dayprayermapconv(previousdayprayers)
fmt.printf("first goroutine result %v\n", data) // todo: remove this line after debug
if err != nil {
fmt.printf("first goroutine err != nil %v\n", err)
pdayerrch <- err
}
fmt.printf("first goroutine putting data into channel")
pdayresch <- data
fmt.printf("first go routine finished") // todo: remove this line after debug
wg.done()
}()
pdayerr := <-pdayerrch
close(pdayerrch)
if pdayerr != nil {
return pdayerr
}
fmt.println("pday err finised")
p.previousdayprayers = <-pdayresch
close(pdayresch)
这是打印语句的结果
first goroutine result map[Asr:map[Hour:3 Minute:28] Dhuhr:map[Hour:12 Minute:23] Fajr:map[Hour:5 Minute:32] Isha:map[Hour:7 Minute:5] Maghrib:map[Hour:6 Minute:13]] first goroutine putting data into channel
所以 data 变量中有数据应该传递到 pdayresch 中,但它似乎卡在那里,为什么?
解决方案
由于我检查错误的 goroutine 中的情况
if err != nil {
fmt.Printf("first goroutine err != nil %v\n", err)
pDayErrCh <- err
}
因为错误为零,所以数据永远不会传递到通道中,因此挂起
删除条件并传递错误,无论如何解决问题。
理论要掌握,实操不能落!以上关于《goroutine 被挂起时传递数据》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
