Here I am trying to iterate over res
and launch a goroutine for every item. Inside every goroutine, again I am launching 3 goroutines in a buffered channel.
Running this code blocks the completion and does not allow the programme to complete.
func (aui *AssignmentUtilImpl) MapAssignmentSubmissionData(res []AssignmentSubmissionNode) []AssignmentSubmission {
if res == nil {
return nil
}
submissions := []AssignmentSubmission{}
ch := make(chan string, len(res))
// map data
for _, val := range res {
go func(val AssignmentSubmissionNode) {
sub := AssignmentSubmission{}
c := make(chan string, 3)
go mapSubmission(&sub, val, c)
go mapUser(&sub, val, c)
go mapFiles(&sub, val, c)
sub.AssignmentId = val.AssignmentId
sub.ClassroomId = val.ClassroomId
for l := range c {
fmt.Println(l)
}
close(c)
submissions = append(submissions, sub)
ch <- "submission2: " + sub.Id
}(val)
}
for l := range ch {
fmt.Println(l)
}
close(ch)
return submissions
}
func mapFiles(sub *AssignmentSubmission, val AssignmentSubmissionNode, c chan string) {
for _, f := range val.Files {
file := resourceModule.File{}
mapstructure.Decode(f.Data, &file)
sub.Files = append(sub.Files, file)
}
c <- fmt.Sprintf("files: %d", len(sub.Files))
}
func mapUser(sub *AssignmentSubmission, val AssignmentSubmissionNode, c chan string) {
user := userModule.User{}
mapstructure.Decode(val.User.Data, &user)
sub.User = user
c <- "user: " + user.Id
}
func mapSubmission(sub *AssignmentSubmission, val AssignmentSubmissionNode, c chan string) {
mapstructure.Decode(val.Submission.Data, &sub)
c <- "submission1: " + sub.Id
}