有没有在手边复制内部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}
[Golang可能的重复:是否可以在不同的结构类型之间进行转换?](https://stackoverflow.com/questions/24613271/golang -is-conversion-between-different-struct-types-possible) –
你不能使用。 box:= rated.Box ?? –
也有关:https://stackoverflow.com/a/37725577/19020 –