I'm trying to clone/copy a html Node so that I can modify/duplicate it and then inject it back in the main document. The issue is that I'm getting a stack overflow[2]. I assume there is a race condition. It looks like it's due Parent
and PrevSibling
fields(based on my blind tests). Any idea why is that and how can I clone it completely(so that it can test positive on reflect.DeepEqual) ?
func clone(src *html.Node) *html.Node {
if src == nil {
return nil
}
n := html.Node{
Parent: clone(src.Parent),
FirstChild: clone(src.FirstChild),
LastChild: clone(src.LastChild),
PrevSibling: clone(src.PrevSibling),
NextSibling: clone(src.NextSibling),
Type: src.Type,
DataAtom: src.DataAtom,
Data: src.Data,
Namespace: src.Namespace,
}
for _, v := range n.Attr {
n.Attr = append(n.Attr, v)
}
return &n
}
[2]
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
runtime stack:
runtime.throw(0x3495b9, 0xe)
/Users/x/gosrc/src/runtime/panic.go:566 +0x95
runtime.newstack()
/Users/x/gosrc/src/runtime/stack.go:1061 +0x416
runtime.morestack()
/Users/x/gosrc/src/runtime/asm_amd64.s:366 +0x7f
goroutine 7 [running]:
runtime.heapBitsSetType(0xc42a0ae5b0, 0x70, 0x68, 0x32e420)
/Users/x/gosrc/src/runtime/mbitmap.go:867 fp=0xc4402002a8 sp=0xc4402002a0
runtime.mallocgc(0x70, 0x32e420, 0x1, 0x0)
/Users/x/gosrc/src/runtime/malloc.go:690 +0x5ba fp=0xc440200348 sp=0xc4402002a8
runtime.newobject(0x32e420, 0x0)
/Users/x/gosrc/src/runtime/malloc.go:785 +0x38 fp=0xc440200378 sp=0xc440200348
bitbucket.org/x/y/client.clone(0xc420138d20, 0x0)