douhe3313 2017-03-05 17:45
浏览 39


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


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
    /Users/x/gosrc/src/runtime/stack.go:1061 +0x416
    /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, 0x0)
  • 写回答

1条回答 默认 最新

  • dsplos5731 2017-03-05 18:06

    When deep-cloning a data structure containing pointers that is not a tree you need a more sophisticated approach; if you call

     n := Node{...

    and your structure has even just two siblings nodes n1 and n2 where is &n2 and n2.prev is &n1 the code will stack overflow (cloning n1 will call clone(n2) for the next pointer that in turn will call clone(n1) for the prev pointer, looping back and forth forever until the call stack explodes).

    A solution is to keep a "cache" where when cloning a node you will store the src→cloned association, so the clone procedure will be able to return the node in case of recursive structures.

    What follows is a full minimal example:

    package main
    import "fmt"
    type Node struct {
        value      int
        prev, next *Node
    func clone(n *Node, cache map[*Node]*Node) *Node {
        if n == nil {
            return nil
        if val, ok := cache[n]; ok {
            return val
        val := &Node{}
        cache[n] = val
        val.value = n.value
        val.prev = clone(n.prev, cache) = clone(, cache)
        return val
    func printlist(n *Node) {
        for n != nil {
            println(fmt.Sprintf("address=%p, value=%v, prev=%p, next=%p",
                n, n.value, n.prev,
            n =
    func main() {
        n1 := &Node{}
        n2 := &Node{}
        n3 := &Node{}
        n1.value = 100
        n2.value = 200
        n3.value = 300 = n2
        n2.prev = n1 = n3
        n3.prev = n2
        println("Cloning list")
        c1 := clone(n1, make(map[*Node]*Node))

    Running this program on my machine i get

    ~/x$ go run recstruct.go
    address=0xc42000e540, value=100, prev=0x0, next=0xc42000e560
    address=0xc42000e560, value=200, prev=0xc42000e540, next=0xc42000e580
    address=0xc42000e580, value=300, prev=0xc42000e560, next=0x0
    Cloning list
    address=0xc42000e5c0, value=100, prev=0x0, next=0xc42000e5e0
    address=0xc42000e5e0, value=200, prev=0xc42000e5c0, next=0xc42000e600
    address=0xc42000e600, value=300, prev=0xc42000e5e0, next=0x0

    where you can see that the three nodes have been cloned correctly and prev/next are pointing each other in the cloned structure list.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥20 unity内置语言切换的按钮设置
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器