func loadComments(root intTree) commentTree { result := commentTree{} var wg sync.WaitGroup loadCommentsInner(&result, root, &wg) wg.Wait() return result } func loadCommentsInner(resNode *commentTree, node intTree, wg *sync.WaitGroup) { wg.Add(len(node.children)) for _, res := range node.children { child := &commentTree{} resNode.children = append(resNode.children, child) res := res go func() { defer wg.Done() loadCommentsInner(child, res, wg) }() } resNode.value = getCommentById(node.id) }