I had deep dived in some comparisons about deep or shallow copy while passing a struct with primitive and pointer fields. Like:
type Copy struct {
age int
ac *AnotherCopy
}
type AnotherCopy struct {
surname string
}
func main() {
s := Copy{
age: 20,
ac: &AnotherCopy{surname: "Relic"},
}
passIt(&s)
fmt.Printf("main s: %p\n", &s)
}
func passIt(s *Copy) {
f := *s
fmt.Printf("s: %p\n", &*s)
fmt.Printf("f: %p\n", &f)
f.age = 26
f.ac.surname = "Walker"
fmt.Printf("%v %s\n", f, f.ac.surname)
fmt.Printf("%v %s\n", *s, s.ac.surname)
}
The result is
s: 0xc000010230
f: 0xc000010250
{26 0xc000010240} Walker
{20 0xc000010240} Walker
main s: 0xc000010230
What I can see here is that, when we pass a struct with primitive and composite types, It copies deeply primitive types and copies shallowly pointer (reference) fields.
I have read some articles about that process and there is a conflict between thoughts.
The question is what should we call that process? Deep copy or shallow copy? and if we call the process as only shallow copy, is this wrong? Can you clarify me please?