Can a Go struct inherit a set of values from a type of another struct?
Something like this.
type Foo struct {
Val1, Val2, Val3 int
}
var f *Foo = &Foo{123, 234, 354}
type Bar struct {
// somehow add the f here so that it will be used in "Bar" inheritance
OtherVal string
}
Which would let me do this.
b := Bar{"test"}
fmt.Println(b.Val2) // 234
If not, what technique could be used to achieve something similar?
Here’s how you may embed the Foo struct in the Bar one :
Now suppose you don’t want the values to be copied and that you want
bto change iffchanges. Then you don’t want embedding but composition with a pointer :Two different kind of composition, with different abilities.