This is part of real world code I'm trying to write. The problem is that Go copies the interface sibling
so I can't modify the data. However, if I change to using a pointer to an interface then the concept of equality fails. I know I can use DeapEquals
, but not in a map.
package main
import (
"fmt"
)
type Q interface {
modify()
}
type P struct {
name string
sibling Q
}
func (x P) modify() {
x.name = "a"
}
func main() {
a := P{"a", nil}
A := P{"?", nil}
b := P{"b", a}
B := P{"b", A}
B.sibling.modify()
fmt.Println(B)
fmt.Println(b == B)
}
How do I get Go to let me modify the interface data itself without copying it and modifying the copy?
It seems these are mutually exclusive on a struct:
- I need to be able to use maps
- I need to be able to modify the interface data with methods