如何在for循环中同时初始化两个变量?
时间:2024-02-24
浏览:496
作者:佚名
“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《如何在for循环中同时初始化两个变量?》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!
问题内容
在四循环中初始化
目标是添加两棵树并返回一棵新树,在下面的代码中:
/*
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循环语法从两棵树中获取分支?
解决方案
您可以使用以下方法实现此目的:
for branch1, branch2 := firstTreeBranches[0], secondTreeBranches[0] ; ; {
...
}
Try it on the playground。更多信息请参阅Variable declaration。
理论要掌握,实操不能落!以上关于《如何在for循环中同时初始化两个变量?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
热门专题
