Skip to content

Incorrect nil copy #2

Open
Open
@alresvor

Description

@alresvor

https://go.dev/play/p/DwZgPX8HhBs

package main

import (
	"fmt"

	"golang.design/x/reflect"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func main() {
	n := ListNode{Val: 1}
	n2 := reflect.DeepCopy(n)
	fmt.Printf("%+v %+v\n", n, n2)
	fmt.Println(n2.Next.Val) // 0
}

n.Next is nil, but becomes &ListNode{} after the deep copy.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions