💻 Tech
In Go, interface stores its own copy of the values. If using value semantics, interface value creates its own copy of the value. On the other hand, for pointer semantics, interface value creates its own copy of the address.
In the example below, name property of p1
and p2
structs was changed, and we can see that only p2
has its name changed when print()
was called. However, both of them have their name changed when the values were printed outside the slice.
This demonstrates that interface values operate on the data differently depending on the semantics of the concrete implementation.
type printer1 struct{ name string }
type printer2 struct{ name string }
func (p printer1) print() {
fmt.Println(p.name)
}
func (p *printer2) print() {
fmt.Println(p.name)
}
func main() {
p1 := printer1{name: "printer 1"}
p2 := printer2{name: "printer 2"}
// slice of interface concrete implementations
// an interface value stores its own copy of value
// so changes to the value outside the slice will not be detected
// however, for pointers, interface value has its own copy of the address
// so changes to the original value will be detected
printers := []printer{
p1,
&p2,
}
p1.name = "printer 1 changed"
p2.name = "printer 2 changed"
for _, p := range printers {
print(p)
}
fmt.Println(p1.name)
fmt.Println(p2.name)
}
func print(p printer) {
p.print()
}
printer 1
printer 2 changed
printer 1 changed
printer 2 changed