I am trying to find out in a := b if a will be a different copy of struct like func pass_by_value(a some_struct)
But I find that I don't understand the print statement.
Consider this go playground
nested_level2 := test_assign_nested_level2{}
nested_level1 := test_assign_nested{nested_level2}
top_level := test_assign{nested_level1}
assign := top_level
fmt.Println("top_level address")
fmt.Printf("%p", &top_level)
fmt.Println(" ")
fmt.Println("1 level address")
fmt.Printf("%p", &top_level.Level1)
fmt.Println(" ")
fmt.Println("2 level address")
fmt.Printf("%p", &top_level.Level1.Level_2)
fmt.Println("------------------------")
fmt.Println("assign top_level address")
fmt.Printf("%p", &assign)
fmt.Println(" ")
fmt.Println("1 level address")
fmt.Printf("%p", &assign.Level1)
fmt.Println(" ")
fmt.Println("2 level address")
fmt.Printf("%p", &assign.Level1.Level_2)
The output of the above is
top_level address
0x10410020
1 level address
0x10410020
2 level address
0x10410020
assign top_level address
0x10410024
1 level address
0x10410024
2 level address
0x10410024
I expect the output to be similar to
fmt.Println("top_level address")
fmt.Printf("%p", &top_level)
fmt.Println(" ")
fmt.Println("1 level address")
fmt.Printf("%p", &nested_level1)
fmt.Println(" ")
fmt.Println("2 level address")
fmt.Printf("%p", &nested_level2)
fmt.Println(" ")
fmt.Println(" ------------------------------- ")
where
top_level address
0x421152280
1 level address
0x421152270
2 level address
0x421152260
each struct have a different address. but it seems like the child struct have the same address as the parent struct.
Why does all the nested element in the struct have the same address?
And does := actually copy a new struct recursively? Like the print statement indicated? (ie. := will return a brand new copy of struct with each of its field content also a brand new copy recursively)
top_level, address oftop_level. Level1top_level.Level1.Level_2etc..... Can you explain why they are all the same?assignhas a different address thantop_levelsince now i know that:=will make a new copy of the operand recursively, but it does not make sense to have all the nested struct to have the same address as the parent struct. I must be getting something wrong here.