gpt4 book ai didi

go - 如何在for循环中使用两个initStmt?

转载 作者:行者123 更新时间:2023-12-01 22:43:09 26 4
gpt4 key购买 nike

Initstmt在循环中
目标是在下面的代码中添加两棵树并返回一棵新树:

/*
3 3
/ \ / \
/ \ / \
1 2 1 2
/ \ /
/ \ /
1 1 1

*/

/*
1) Tree has root label and list of branches
2) Each branch is again a tree
*/

type Tree struct {
rootLabel int
branches []Tree
}

func addTrees(t1 Tree, t2 Tree) Tree {
if isLeaf(t1) {
return Tree{}
}
firstTreeBranches := branches(t1)
secondTreeBranches := branches(t2)
for branch1:= firstTreeBranches[0], branch2:= secondTreeBranches[0];;{

}
}

func branches(t Tree) []Tree {
return t.branches
}

我想进行递归调用 addTrees(branch1,branch2)在上面的代码中。
如何使用 for-loop 语法从两棵树中获取分支?

最佳答案

您可以使用以下方法实现此目的:

for branch1, branch2 := firstTreeBranches[0], secondTreeBranches[0] ; ; {
...
}
Try it on the playground .引用 Variable declaration了解更多信息。

关于go - 如何在for循环中使用两个initStmt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63542065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com