copy icon indicating copy to clipboard operation
copy copied to clipboard

Copy function does not appear to deep copy

Open dschreij opened this issue 4 years ago • 0 comments

If you have the following structs:

type User struct {
    Name string
    Email string
    Company *Company
}

type Company struct {
    Name string
    Domain string
}

and do the following:

user1 := &User{
    Name: "Someone",
    Email: "[email protected]",
    Company: &Company{
        Name: "Some co.",
        Domain: "example.com"
   },
}

var user2 User
copy.Copy(&user2, user1)

The pointer contained in user1 is copied to user2. If I now do user2.Company.Name = "Something else", the value is also changed in user1. Is this intended? In that case, your library is not intended for use as a deep copier? It may be nice to offer the option whether to deepcopy or not as a parameter.

Thanks for all the work you put in this library!

dschreij avatar Nov 01 '21 06:11 dschreij