I'm working through "The Go Programming Language" book and have come across what is an unusual for loop syntax in chapter 5. I've cut down the example below, but the whole program is on the book's GitHub page.
type Node struct {
int NodeType
FirstChild, NextSibling *Node
}
func visit(n *Node) {
for c:= n.FirstChild; c != nil; c = c.NextSibling {
visit(c)
}
}
The C parser in my head tells me that c.NextSibling
will always either point to a Node
or be nil
. In that case the loop should either then always break or continue forever.
When c.NextSibling
is not nil
, it seems that the loop is exiting as the loop value is the same as the previous iteration, but I couldn't find anything in the Go Language Specification to back that up.
I've compiled that program and confirm that it works as per the book.
Am I missing something fundamental or is something else going on here?
Full example, with instrumented code (thanks to Sergio):
package main
import (
"fmt"
)
type Node struct {
NodeId int
FirstChild, NextSibling *Node
}
func visit(n *Node) {
fmt.Printf("Entering node %d
", n.NodeId)
for c := n.FirstChild; c != nil; c = nextSib(c) {
fmt.Printf("Will now visit node %d
", c.NodeId)
visit(c)
}
}
func nextSib(n *Node) *Node {
next := n.NextSibling
fmt.Printf("In nextSib for %d %t
", n.NodeId, next != nil)
return next
}
func main() {
c4 := &Node{NodeId: 5}
c3 := &Node{NodeId: 4}
c2 := &Node{NodeId: 3, NextSibling: c3}
c1 := &Node{NodeId: 2, FirstChild: c4, NextSibling: c2}
root := &Node{NodeId: 1, FirstChild: c1}
visit(root)
}
Output:
Entering node 1
Will now visit node 2
Entering node 2
Will now visit node 5
Entering node 5
In nextSib for 5 false
In nextSib for 2 true
Will now visit node 3
Entering node 3
In nextSib for 3 true
Will now visit node 4
Entering node 4
In nextSib for 4 false