From c4300d7d840949c8122019edf58199faf6e9a73f Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Mon, 3 Sep 2012 21:17:13 -0600 Subject: [PATCH] closure-free simple walk --- exercises/08-ebt/go.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/exercises/08-ebt/go.go b/exercises/08-ebt/go.go index 9654635..48e9c39 100644 --- a/exercises/08-ebt/go.go +++ b/exercises/08-ebt/go.go @@ -4,22 +4,18 @@ 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) { - // 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 { - return - } - walk(t.Left) - ch <- t.Value - walk(t.Right) - } - walk(t) + walk(t, ch) close(ch) }