亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

發送通道后 Goroutine 未執行

發送通道后 Goroutine 未執行

Go
Helenr 2023-08-14 17:40:56
package mainimport (    "fmt"    "sync")// PUT functionfunc put(hashMap map[string](chan int), key string, value int, wg *sync.WaitGroup) {    defer wg.Done()    fmt.Printf("this is getting printed")    hashMap[key] <- value    fmt.Printf("this is not getting printed")    fmt.Printf("PUT sent %d\n", value)}func main() {    var value int    var key string    wg := &sync.WaitGroup{}    hashMap := make(map[string](chan int), 100)    key = "xyz"    value = 100    for i := 0; i < 5; i++ {        wg.Add(1)        go put(hashMap, key, value, wg)    }    wg.Wait()}put 函數中的最后兩個打印語句沒有被打印,我試圖根據鍵將值放入映射中。以及在這種情況下如何關閉 hashMap。
查看完整描述

3 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

  1. 您需要創建一個頻道,例如hashMap[key] = make(chan int)

  2. 由于您沒有從通道中讀取數據,因此需要緩沖通道才能使其工作:

    key := "xyz"
    hashMap[key] = make(chan int, 5)

嘗試以下代碼:

func put(hashMap map[string](chan int), key string, value int, wg *sync.WaitGroup) {

    hashMap[key] <- value

    fmt.Printf("PUT sent %d\n", value)

    wg.Done()

}

func main() {

    var wg sync.WaitGroup

    hashMap := map[string]chan int{}

    key := "xyz"

    hashMap[key] = make(chan int, 5)

    for i := 0; i < 5; i++ {

        wg.Add(1)

        go put(hashMap, key, 100, &wg)

    }

    wg.Wait()

}

輸出:


PUT sent 100

PUT sent 100

PUT sent 100

PUT sent 100

PUT sent 100


查看完整回答
反對 回復 2023-08-14
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

我解決該問題的解決方案是:


// PUT function

func put(hashMap map[string](chan int), key string, value int, wg *sync.WaitGroup) {

    defer wg.Done()

    fmt.Printf("this is getting printed")

    hashMap[key] <- value // <-- nil problem

    fmt.Printf("this is not getting printed")

    fmt.Printf("PUT sent %d\n", value)

}

在函數的這行代碼中hashMap[key] <- value,put它不能接受,value因為它chan int是nil在參數中定義的put (hashMap map[string](chan int)。


// PUT function

func put(hashMap map[string](chan int), cval chan int, key string, value int, wg *sync.WaitGroup) {

    defer wg.Done()

    fmt.Println("this is getting printed")

    cval <- value // put the value in chan int (cval) which is initialized

    hashMap[key] = cval // set the cval(chan int) to hashMap with key

    fmt.Println("this is not getting printed")

    fmt.Printf("PUT sent %s %d\n", key, value)

}


func main() {

    var value int

    wg := &sync.WaitGroup{}


    cval := make(chan int,100)

    hashMap := make(map[string](chan int), 100)


    value = 100

    for i := 0; i < 5; i++ {

        wg.Add(1)

        go put(hashMap, cval, fmt.Sprintf("key%d",i), value, wg)

    }

    wg.Wait()


    /* uncomment to test cval 

    close(cval)


    fmt.Println("Result:",<-hashMap["key2"])

    fmt.Println("Result:",<-hashMap["key1"])

    cval <- 88 // cannot send value to a close channel

    hashMap["key34"] = cval

    fmt.Println("Result:",<-hashMap["key1"])

    */



}

在我的代碼示例中。我將cval緩沖通道 100 初始化為相同大小,hashMap并將 cval 作為put函數中的值傳遞。您只能關閉cval而不是 hashMap 本身。


查看完整回答
反對 回復 2023-08-14
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

另外,您的代碼可以簡化為這樣。為什么要傳遞不必要的參數!一項額外的修改是我采用了不同的值,以使您更清楚地理解這個概念。


package main


import (

    "log"

    "sync"

)


func put(hash chan int, wg *sync.WaitGroup) {

    defer wg.Done()

    log.Println("Put sent: ", <-hash)

}


func main() {

    hashMap := map[string]chan int{}

    key := "xyz"

    var wg sync.WaitGroup

    hashMap[key] = make(chan int, 5)

    for i := 0; i < 5; i++ {

        value := i

        wg.Add(1)

        go func(val int) {

            hashMap[key] <- val

            put(hashMap[key], &wg)

        }(value)

    }

    wg.Wait()

}


查看完整回答
反對 回復 2023-08-14
  • 3 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號