bundle fundamental
import (
“fmt”
“math/rand”
“sync”
)
func drG(wg *sync.WaitGroup, c chan int) func(int, int, int) {
return func(min, max, itr int) {
wg.Add(1)
defer wg.Carried out()
for i := 0; i <= itr; i++ {
c <- rand.Intn(max-min) + max
}
shut(c)
wg.Wait()
}
}
func fundamental() {
wg := sync.WaitGroup{}
c1, c2 := make(chan int), make(chan int)
ar_c1, ar_c2 := int{}, int{}
go drG(&wg, c1)(10, 99, 2)
go drG(&wg, c1)(111, 999, 3)
go drG(&wg, c2)(0, 9, 3)
for i := vary c1 {
ar_c1 = append(ar_c1, i)
}
for i := vary c2 {
ar_c2 = append(ar_c2, i)
}
fmt.Println(ar_c1, ar_c2)
}
Hey gophers.
I’m very new in programming world and am doing this as a passion. I’ve learn on goroutines and channels and thought I’d give it a strive solely to come back throughout this problem
I get beneath error:
[166 187 121] [15 14 10 17]
panic: ship on closed channel
goroutine 7 [running]:
fundamental.fundamental.drG.func2(0x6f, 0x3e7, 0x3)
/tmp/sandbox3358109023/prog.go:17 +0xb5
created by fundamental.fundamental in goroutine 1
/tmp/sandbox3358109023/prog.go:31 +0x19b
Program exited.
Once I use goroutine with a single c2 channel, it’s working with no errors, on utilizing two goroutines with identical channels, I get the; panic: ship on closed channel error.
How do I overcome this. Apologies if this can be a primary inquiries to you guys