I am playing around with struct embedding and have a problem with keeping the same reference to the embedded struct.
Try out Go Playground and see that there are two different pointer addresses to *strings.Reader
.
package main
import (
"fmt"
"strings"
)
type Base struct {
reader *strings.Reader
}
func NewBase() *Base {
r := strings.NewReader("hello")
fmt.Printf("document: %#+v
", &r)
return &Base{r}
}
func (b *Base) Check() {
fmt.Printf("document: %#+v
", &b.reader)
}
type Concrete struct {
*Base
}
func NewConcrete() *Concrete {
return &Concrete{NewBase()}
}
func main() {
c := NewConcrete()
c.Check()
}
Why are these addresses not the same? How do I fix this?