两个包之间的通道通信死锁 - Golang

两个包之间的通道通信死锁 - Golang。


我有两个包通过两个渠道进行通信。一个是主要的,另一个是功能。当我运行它时,我会遇到死锁。


package main


import (

    functionspackage "GoEjemplos/subFolder"

    "fmt"

    "sync"

)


func main() {

    var wg sync.WaitGroup


    ChannelSendData := make(chan functionspackage.FunctionStruct, 1)

    defer close(ChannelSendData)


    data := functionspackage.FunctionStruct{

        FieldOne: 3.56,

        FieldTwo: 23,

    }


    ChannelSendData <- data


    wg.Add(1)

    go functionspackage.FunctionExt(ChannelSendData, &wg)


    recibe := <-functionspackage.ChannelOutFunct


    fmt.Println("channelOut: ", recibe)


    close(functionspackage.ChannelOutFunct)


    wg.Wait()

}


另一个包是


package functionspackage


import "sync"


type FunctionStruct struct {

    FieldOne float64

    FieldTwo int

}


var ChannelOutFunct chan float64


func FunctionExt(RecibeChan chan FunctionStruct, wg *sync.WaitGroup) (ChannelOutFunct chan float64) {


    reciveData := <-RecibeChan


    result := reciveData.FieldOne * float64(reciveData.FieldTwo)


    ChannelOutFunct <- result


    wg.Done()


    return ChannelOutFunct

}


这就是僵局。


PS C:\Go-Project\src\GoEjemplos> go run main.go

fatal error: all goroutines are asleep - deadlock!


goroutine 1 [chan receive (nil chan)]:

main.main()

        C:/Go-Project/src/GoEjemplos/main.go:32 +0x13d


goroutine 19 [chan send (nil chan)]:

GoEjemplos/subFolder.FunctionExt(0xc0000d4000, 0xc0000a2070, 0xc0000c9f18)

        C:/Go-Project/src/GoEjemplos/subFolder/functionsPackage.go:19 +0x85

created by main.main

        C:/Go-Project/src/GoEjemplos/main.go:30 +0x11a

exit status 2

PS C:\Go-Project\src\GoEjemplos>

你能给我解释一下问题出在哪里吗?


繁星点点滴滴
浏览 104回答 2
2回答

慕无忌1623718

通道functionspackage.ChannelOutFunct未初始化,因此它是一个零通道。写入零通道或从零通道读取将始终阻塞。https://dave.cheney.net/2014/03/19/channel-axioms

牧羊人nacy

我让它工作。这是代码package mainimport (&nbsp; &nbsp; packagefunctions "GoEjemplos/subFolder"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "sync")func main() {&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; var ChannelSendData = make(chan packagefunctions.FunctionStruct, 0)&nbsp; &nbsp; defer close(ChannelSendData)&nbsp; &nbsp; var ChanReturn = make(chan float64)&nbsp; &nbsp; defer close(ChanReturn)&nbsp; &nbsp; data := packagefunctions.FunctionStruct{&nbsp; &nbsp; &nbsp; &nbsp; FieldOne: 3.56,&nbsp; &nbsp; &nbsp; &nbsp; FieldTwo: 2,&nbsp; &nbsp; }&nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; go func() { ChannelSendData <- data }()&nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; ChanReturn = packagefunctions.FunctionExt(ChannelSendData, &wg)&nbsp; &nbsp; &nbsp; &nbsp; recibeChanReturn := <-ChanReturn&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("channelOut: ", recibeChanReturn)&nbsp; &nbsp; &nbsp; &nbsp; wg.Done()&nbsp; &nbsp; }()&nbsp; &nbsp; wg.Wait()}另一个包是package packagefunctionsimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "sync")type FunctionStruct struct {&nbsp; &nbsp; FieldOne float64&nbsp; &nbsp; FieldTwo int}func FunctionExt(ChanIn chan FunctionStruct, wg *sync.WaitGroup) chan float64 {&nbsp; &nbsp; reciveData, ok := <-ChanIn&nbsp; &nbsp; if ok == false {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("channel closed")&nbsp; &nbsp; }&nbsp; &nbsp; var result float64&nbsp; &nbsp; result = reciveData.FieldOne * float64(reciveData.FieldTwo)&nbsp; &nbsp; var ChannelReturn = make(chan float64, 1)&nbsp; &nbsp; defer close(ChannelReturn)&nbsp; &nbsp; ChannelReturn <- result&nbsp; &nbsp; wg.Done()&nbsp; &nbsp; return ChannelReturn}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go