第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

兩個包之間的通道通信死鎖 - Golang

兩個包之間的通道通信死鎖 - Golang

Go
繁星點點滴滴 2022-06-27 17:20:56
兩個包之間的通道通信死鎖 - Golang。我有兩個包通過兩個渠道進行通信。一個是主要的,另一個是功能。當我運行它時,我會遇到死鎖。package mainimport (    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 functionspackageimport "sync"type FunctionStruct struct {    FieldOne float64    FieldTwo int}var ChannelOutFunct chan float64func 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.gofatal error: all goroutines are asleep - deadlock!goroutine 1 [chan receive (nil chan)]:main.main()        C:/Go-Project/src/GoEjemplos/main.go:32 +0x13dgoroutine 19 [chan send (nil chan)]:GoEjemplos/subFolder.FunctionExt(0xc0000d4000, 0xc0000a2070, 0xc0000c9f18)        C:/Go-Project/src/GoEjemplos/subFolder/functionsPackage.go:19 +0x85created by main.main        C:/Go-Project/src/GoEjemplos/main.go:30 +0x11aexit status 2PS C:\Go-Project\src\GoEjemplos>你能給我解釋一下問題出在哪里嗎?
查看完整描述

2 回答

?
慕無忌1623718

TA貢獻1744條經(jīng)驗 獲得超4個贊

通道functionspackage.ChannelOutFunct未初始化,因此它是一個零通道。寫入零通道或從零通道讀取將始終阻塞。

https://dave.cheney.net/2014/03/19/channel-axioms


查看完整回答
反對 回復 2022-06-27
?
牧羊人nacy

TA貢獻1862條經(jīng)驗 獲得超7個贊

我讓它工作。這是代碼


package main


import (

    packagefunctions "GoEjemplos/subFolder"

    "fmt"

    "sync"

)


func main() {

    var wg sync.WaitGroup

    var ChannelSendData = make(chan packagefunctions.FunctionStruct, 0)

    defer close(ChannelSendData)

    var ChanReturn = make(chan float64)

    defer close(ChanReturn)


    data := packagefunctions.FunctionStruct{

        FieldOne: 3.56,

        FieldTwo: 2,

    }


    wg.Add(1)

    go func() { ChannelSendData <- data }()


    wg.Add(1)

    go func() {

        ChanReturn = packagefunctions.FunctionExt(ChannelSendData, &wg)

        recibeChanReturn := <-ChanReturn

        fmt.Println("channelOut: ", recibeChanReturn)

        wg.Done()

    }()

    wg.Wait()

}

另一個包是


package packagefunctions


import (

    "fmt"

    "sync"

)


type FunctionStruct struct {

    FieldOne float64

    FieldTwo int

}


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

    reciveData, ok := <-ChanIn

    if ok == false {

        fmt.Println("channel closed")

    }

    var result float64

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

    var ChannelReturn = make(chan float64, 1)

    defer close(ChannelReturn)

    ChannelReturn <- result

    wg.Done()

    return ChannelReturn

}


查看完整回答
反對 回復 2022-06-27
  • 2 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號