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

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

發送通道后 Goroutine 未執行

發送通道后 Goroutine 未執行

Go
拉風的咖菲貓 2022-05-23 14:52:38
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貢獻1785條經驗 獲得超4個贊

  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


查看完整回答
反對 回復 2022-05-23
?
縹緲止盈

TA貢獻2041條經驗 獲得超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是。nilput (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 本身。


查看完整回答
反對 回復 2022-05-23
?
翻閱古今

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

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


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()

}


查看完整回答
反對 回復 2022-05-23
  • 3 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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