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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化