In my code , I have a function which is getting data from the database for different ids , so i put in the go routine for each id , but in case if any go routine face any error then how it will be handled .I stuck on it .
Below is the snippet:
func BStat(ID string) (t []string, err error) {
var wg sync.WaitGroup
var e error
ch1 := make(chan string)
ch2 := make(chan error)
defer close(ch1)
defer close(ch2)
for i:=0 ; i <10 :i++ {
wg.Add(1)
go GetStat(ID, ch1, ch2 , &wg)
}
for i:=0 ; i <10 :i++{
e = <- ch2
if e != nil { // How to Handle error
fmt.Println("Heloooooooooooooooo")
break
//return nil, errors.New("error in generating report ")
}
Stat, ok := <-ch1
if ok == true {
t = append(t, Stat)
}
}
wg.Done()
return t, e
}
func GetStat( Id uint64, ch1 chan<- []string , ch2 chan<- bool , wg *sync.WaitGroup) {
stat, err := store.GetName(db, i) // Value from the db
if err != nil {
logger.WithError(err)
ch2 <- true
ch1 <- ""
}
ch2 <- false
ch1 <- stat
}
My go routine stuck , Please suggest me how to handle the o/p and to handle the error case .
Thanks In Advance