1
0
Fork 0

okay, so I liked the closure way, just prefer this slightly more compact spelling

This commit is contained in:
Stephen M. McQuay 2012-09-03 21:18:02 -06:00
parent c4300d7d84
commit 916def1c5e
1 changed files with 11 additions and 8 deletions

View File

@ -4,18 +4,21 @@ import (
"code.google.com/p/go-tour/tree"
"fmt"
)
func walk(t *tree.Tree, ch chan int) {
if t != nil {
walk(t.Left, ch)
ch <- t.Value
walk(t.Right, ch)
}
}
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
walk(t, ch)
// do it as a nested closure so that I can control the call to close so
// that I can use the range-based forloop in main.main
var walk func(t *tree.Tree)
walk = func(t *tree.Tree) {
if t == nil {
walk(t.Left)
ch <- t.Value
walk(t.Right)
}
}
walk(t)
close(ch)
}