Hey, All: I have made a little testing about interface assignment and searched the source code about interface runtime functions like convI2I, and notice that there are two situations where data may be copied or not: (1) data not copied: assign another* interface type* variable to the targeted interface variable (2) data copied and memory allocated: assign another *not interface type *variable to the targeted interface variable
I wonder are there any special reasons for that? Thanks in advance! following is my simple testing code and debug info // interface.go package main import "fmt" type Data struct { n int } func main() { d := Data{10} fmt.Printf("address of d: %p\n", &d) // assign not interface type variable to interface variable // d will be copied var i1 interface{} = d // assign interface type variable to interface variable // the data of i1 will directly assigned to i2.data and will not be copied var i2 interface{} = i1 fmt.Println(d) fmt.Println(i1) fmt.Println(i2) } // build without optimization and inline go build -gcflags "-N -l" interface.go // the data address of i1 and i2 are the same (gdb) info locals &d = 0xc420074168 i2 = {_type = 0x492e00, data = 0xc4200741a0} i1 = {_type = 0x492e00, data = 0xc4200741a0} -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.