有沒有在手邊複製內部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 –