I'm doing the Go Tour: http://tour.golang.org/#72 This is my code:
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
var makeWalk func(t *tree.Tree, ch chan int)
makeWalk = func(t *tree.Tree, ch chan int) {
if t.Left != nil {
makeWalk(t.Left, ch)
}
fmt.Println("-->", t.Value)
ch <- t.Value
fmt.Println("continue here")
if t.Right != nil {
makeWalk(t.Right, ch)
}
}
makeWalk(t, ch)
close(ch)
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
// var ch_l chan int = make(chan int)
// var ch_r chan int = make(chan int)
return false
}
func main() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for i := range(ch) {
fmt.Println(i)
}
}
This is the output:
--> 1
continue here
--> 2
1
2
continue here
--> 3
continue here
--> 4
3
4
continue here
--> 5
continue here
--> 6
5
6
continue here
--> 7
continue here
--> 8
7
8
continue here
--> 9
continue here
--> 10
9
10
continue here
As far as I understand channels block when they are passed with values. I expect to see an output like this:
--> 1
1
continue here
--> 2
2
continue here
...
--> 10
10
continue here
Channels are not buffered, is fmt.Println
buffered? What happens here? :)