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 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥15 抖音看过的视频,缓存在哪个文件