发送通道后 Goroutine 未执行

package main


import (

    "fmt"

    "sync"

)


// 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

    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。


拉风的咖菲猫
浏览 98回答 3
3回答

九州编程

您需要创建一个频道,例如hashMap[key] = make(chan int)由于您不是从通道读取,因此需要缓冲通道才能使其工作:&nbsp;key := "xyz"&nbsp; &nbsp; hashMap[key] = make(chan int, 5)试试下面的代码:func put(hashMap map[string](chan int), key string, value int, wg *sync.WaitGroup) {&nbsp; &nbsp; hashMap[key] <- value&nbsp; &nbsp; fmt.Printf("PUT sent %d\n", value)&nbsp; &nbsp; wg.Done()}func main() {&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; hashMap := map[string]chan int{}&nbsp; &nbsp; key := "xyz"&nbsp; &nbsp; hashMap[key] = make(chan int, 5)&nbsp; &nbsp; for i := 0; i < 5; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go put(hashMap, key, 100, &wg)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()}输出:PUT sent 100PUT sent 100PUT sent 100PUT sent 100PUT sent 100

缥缈止盈

我解决问题的方法是:// PUT functionfunc put(hashMap map[string](chan int), key string, value int, wg *sync.WaitGroup) {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; fmt.Printf("this is getting printed")&nbsp; &nbsp; hashMap[key] <- value // <-- nil problem&nbsp; &nbsp; fmt.Printf("this is not getting printed")&nbsp; &nbsp; fmt.Printf("PUT sent %d\n", value)}在函数中的这行代码hashMap[key] <- value中put,它不能接受参数中定义的value因为chan int是。nilput (hashMap map[string](chan int)// PUT functionfunc put(hashMap map[string](chan int), cval chan int, key string, value int, wg *sync.WaitGroup) {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; fmt.Println("this is getting printed")&nbsp; &nbsp; cval <- value // put the value in chan int (cval) which is initialized&nbsp; &nbsp; hashMap[key] = cval // set the cval(chan int) to hashMap with key&nbsp; &nbsp; fmt.Println("this is not getting printed")&nbsp; &nbsp; fmt.Printf("PUT sent %s %d\n", key, value)}func main() {&nbsp; &nbsp; var value int&nbsp; &nbsp; wg := &sync.WaitGroup{}&nbsp; &nbsp; cval := make(chan int,100)&nbsp; &nbsp; hashMap := make(map[string](chan int), 100)&nbsp; &nbsp; value = 100&nbsp; &nbsp; for i := 0; i < 5; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go put(hashMap, cval, fmt.Sprintf("key%d",i), value, wg)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; /* uncomment to test cval&nbsp;&nbsp; &nbsp; close(cval)&nbsp; &nbsp; fmt.Println("Result:",<-hashMap["key2"])&nbsp; &nbsp; fmt.Println("Result:",<-hashMap["key1"])&nbsp; &nbsp; cval <- 88 // cannot send value to a close channel&nbsp; &nbsp; hashMap["key34"] = cval&nbsp; &nbsp; fmt.Println("Result:",<-hashMap["key1"])&nbsp; &nbsp; */}在我的代码示例中。我将cval缓冲通道 100 初始化为相同大小,hashMap并将 cval 作为put函数中的值传递。您只能关闭cval而不是 hashMap 本身。

翻阅古今

此外,您的代码可以简化为此。为什么不必要地传递参数!一个额外的修改是我采用不同的值来让你更清楚地理解这个概念。package mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "sync")func put(hash chan int, wg *sync.WaitGroup) {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; log.Println("Put sent: ", <-hash)}func main() {&nbsp; &nbsp; hashMap := map[string]chan int{}&nbsp; &nbsp; key := "xyz"&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; hashMap[key] = make(chan int, 5)&nbsp; &nbsp; for i := 0; i < 5; i++ {&nbsp; &nbsp; &nbsp; &nbsp; value := i&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go func(val int) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashMap[key] <- val&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; put(hashMap[key], &wg)&nbsp; &nbsp; &nbsp; &nbsp; }(value)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go