When attempting to solve tree walk portion of the Equivalent Binary Trees problem in the Go Tour, the obvious solution is to use recursion. Other solutions, such as a closure, are provided in answers to the general question on how to solve the problem.
My original thought was to use a Goroutine for each step of the walk. Isn't this a better, more Go-onic (what's the Go equivalent of Pythonic?) solution? The problem is I couldn't figure out how to either A) close the channel after the tree has been walked, or B) signal in some other way that the tree walk completed. An earlier example uses 2 channels, one for data and one for a quit signal. Passing a second channel doesn't fit with the problem definition, and the root issue of when the walk is finished is still present. Is there an elegant solution with a Goroutine for each walk step, or is recursion the best solution?
func Walk(t *tree.Tree, ch chan int) {
if t != nil {
go Walk(t.Left, ch)
ch <- t.Value
go Walk(t.Right, ch)
}
//Done with this node, how do I know when all are done?
}