【发布时间】:2017-07-26 13:14:22
【问题描述】:
除了手动复制内部Box 值之外,是否还有一种语言功能可以将RatedBox 向下转换为Box?
type Box struct {
Name string
}
type RatedBox struct {
Box
Points int
}
func main() {
rated := RatedBox{Box: Box{Name: "foo"}, Points: 10}
box := Box(rated) // does not work
}
// works, but is quite verbose for structs with more members
box := Box{Name: rated.Name}
【问题讨论】:
-
你不能用。 box :=rated.Box ??
-
@DanEsparza 我看不出我的问题怎么可能是重复的
标签: go struct type-conversion embedding