dongshen9058 2019-02-08 08:46
浏览 45
已采纳

Go for循环的post部分中的分配如何使该循环退出?

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
  • 写回答

1条回答 默认 最新

  • dongqiya9552 2019-02-08 08:59
    关注

    When c.NextSibling is not nil, it seems that the loop is exiting as the loop value is the same as the previous iteration

    Not sure what you meant by that, but yes, you're misinterpreting something. But for loop is not to blame. It most certainly does not exit while its continue condition is still true.

    type Node struct {
        NodeId                  int
        FirstChild, NextSibling *Node
    }
    
    func visit(n *Node) {
        for c := n.FirstChild; c != nil; c = c.NextSibling {
            fmt.Printf("seeing node %d
    ", c.NodeId)
            visit(c)
        }
    }
    
    func main() {
        c3 := &Node{NodeId: 4}
        c2 := &Node{NodeId: 3, NextSibling: c3}
        c1 := &Node{NodeId: 2, NextSibling: c2}
        root := &Node{NodeId: 1, FirstChild: c1}
    
        visit(root)
    }
    

    Output

    seeing node 2
    seeing node 3
    seeing node 4
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?