英文:
golang - Content of a pointer in a slice changes during recursive function run
问题
func getAllCertainDivs(className string, idName string, htmlTag *HtmlTag, matchingDivs *[]*HtmlTag) {
fmt.Println(htmlTag.Class)
if htmlTag.XMLName.Local == "div" {
if htmlTag.Class == className && htmlTag.Id == idName {
*matchingDivs = append(*matchingDivs, htmlTag)
}
}
for _, tag := range htmlTag.ChildTags {
getAllCertainDivs(className, idName, &tag, matchingDivs)
}
}
在上面的函数中,你可以看到,我将一个切片的指针传递给了getAllCertainDivs
函数。在某个点上,一个HtmlTag
指针被添加到了matchingDivs
切片中。在append
之后,我检查了matchingDivs
切片的内容,然后再次递归调用函数自身。然后,在if
语句的下面,我调用函数自身进行递归一次。我停在了fmt.Println(htmlTag.Class)
这一行,并再次检查了matchingDivs
切片的内容。然而,内容与之前完全不同。
只进行了一次append
操作,为什么内容会改变?在每次将HtmlTag
指针传递给下一个递归调用时,golang
是否使用相同的指针?
英文:
func getAllCertainDivs(className string, idName string, htmlTag *HtmlTag, matchingDivs *[]*HtmlTag) {
fmt.Println(htmlTag.Class)
if htmlTag.XMLName.Local == "div" {
if htmlTag.Class == className && htmlTag.Id == idName {
*matchingDivs = append(*matchingDivs, htmlTag)
}
}
for _, tag := range htmlTag.ChildTags {
getAllCertainDivs(className, idName, &tag, matchingDivs)
}
}
In the function above, as you can see, I pass a pointer of a slice into the getAllCertainDivs
function. At a point a HtmlTag
pointer is pushed into the slice matchingDivs
. After the append
I check the content of the matchingDiv slice, before letting the function call itself recursively again. Then below the if where append
was made, the function calls itself recursively one time. And I stop at the fmt.Println(htmlTag.Class)
and check the content of matchingDivs
slice again. And the content is completely different than before.
There has only been one append
, how can the content change ? Does golang
use the same HtmlTag
pointer, everytime I pass it into next recursive call ?
答案1
得分: 1
tag
变量在循环开始时声明一次,并且在每次迭代中被覆盖。这与FAQ中的问题相同:"What happens with closures running as goroutines?"。
您可以在每次迭代期间声明一个新变量,以获得一个唯一的指针用于函数调用:
for _, tag := range htmlTag.ChildTags {
tag := tag
getAllCertainDivs(className, idName, &tag, matchingDivs)
}
或者您可以省略范围值,并直接使用索引:
for i := range htmlTag.ChildTags {
getAllCertainDivs(className, idName, &htmlTag.ChildTags[i], matchingDivs)
}
英文:
The tag
variable is declared once at the start of the loop, and the value of tag
is overwritten on each iteration. This is the same problem you see in the FAQ with: "What happens with closures running as goroutines?"
You can declare a new variable during each iteration to get a unique pointer for the function call:
for _, tag := range htmlTag.ChildTags {
tag := tag
getAllCertainDivs(className, idName, &tag, matchingDivs)
}
Alternatively you can elide the range value, and use the index directly:
for i := range htmlTag.ChildTags {
getAllCertainDivs(className, idName, &htmlTag.ChildTags[i], matchingDivs)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论