charanReddy
charanReddy

Reputation: 177

Goroutine not executing after sending channel

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

The last two print statements in the put function are not getting printed, I am trying to put values into the map based on key.

and also how to close the hashMap in this case.

Upvotes: 0

Views: 238

Answers (3)

wasmup
wasmup

Reputation: 16253

  1. You need to create a channel, for example hashMap[key] = make(chan int)
  2. Since you are not reading from the channel, you need buffered channel to make it work:
    key := "xyz"
    hashMap[key] = make(chan int, 5)

Try the following code:

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

Output:

PUT sent 100
PUT sent 100
PUT sent 100
PUT sent 100
PUT sent 100

Upvotes: 4

Adriel Artiza
Adriel Artiza

Reputation: 337

My solution to fix the problem is:

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

in this line of code hashMap[key] <- value in put function, It cannot accept the value because chan int is nil which is define in put (hashMap map[string](chan int) parameter.

// 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"])
    */


}

In my code example. I initialized cval buffered channel 100 same size to hashMap and pass cval as value in put function. you can close cval only and not the hashMap itself.

Upvotes: 0

shmsr
shmsr

Reputation: 4204

Also, your code can be reduced to this. Why pass params unnecessarily! One extra modification is that I take different values to make you understand the concept clearer.

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

Upvotes: -1

Related Questions