Inheritance Go结构能否继承一组值?

Inheritance Go结构能否继承一组值?,inheritance,go,Inheritance,Go,Go结构能否从另一个结构的类型继承一组值 像这样的 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 } 这样我就可以这样做了 b := Bar{"tes

Go结构能否从另一个结构的类型继承一组值

像这样的

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
}
这样我就可以这样做了

b := Bar{"test"}
fmt.Println(b.Val2) // 234

如果没有,可以使用什么技术来实现类似的功能?

以下是如何将Foo结构嵌入到Bar-one中:

type Foo struct {
    Val1, Val2, Val3 int
}
type Bar struct {
    Foo
    OtherVal string
}
func main() {
    f := &Foo{123, 234, 354}
    b := &Bar{*f, "test"}
    fmt.Println(b.Val2) // prints 234
    f.Val2 = 567
    fmt.Println(b.Val2) // still 234
}
现在假设您不希望复制值,并且如果
f
更改,您希望
b
更改。那么您不希望嵌入,而是希望使用指针进行合成:

type Foo struct {
    Val1, Val2, Val3 int
}
type Bar struct {
    *Foo
    OtherVal string
}
func main() {
    f := &Foo{123, 234, 354}
    b := &Bar{f, "test"}
    fmt.Println(b.Val2) // 234
    f.Val2 = 567
    fmt.Println(b.Val2) // 567
}

两种不同的作文,不同的能力。

谢谢你的回答。这种抄袭是我试图避免的。那么,我是否应该假设最终的答案是,单靠继承是无法实现的?我认为您的第一个解决方案将与我得到的最接近。我脑子里想的是这样的JavaScript
VARProto={val1:123,val2:234,val3:345};var inst=Object.create(proto)