Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created April 30, 2012 06:25
Show Gist options
  • Save minikomi/2555951 to your computer and use it in GitHub Desktop.
Save minikomi/2555951 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func walkAsync(ch chan string, tree *Tree) {
if len(tree.children) == 0 {
ch <- tree.data
return
} else {
for _, c := range tree.children {
walkAsync(ch, c)
}
}
}
type Tree struct {
data string
children []*Tree
}
func main() {
var tree = new(Tree)
tree.children = []*Tree{
new(Tree){"a", make([]*Tree, 0)},
*Tree{"b", make([]*Tree, 0)},
new(Tree),
}
tree.children[2].children = []*Tree{
new(Tree),
new(Tree),
}
tree.children[2].children[0].data = "c"
tree.children[2].children[1].data = "d"
ch := make(chan string)
go func() {
walkAsync(ch, tree)
close(ch)
}()
for c := range ch {
fmt.Println(c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment