我有两个包,其中包 B 导入包 A,如下所示:
套餐A
package A
type Car struct {
Color string
Make string
Model string
}
Run Code Online (Sandbox Code Playgroud)
套餐B
package B
type car struct {
*A.Car
}
func NewCar() car {
return &car{
Color: "red",
Make: "toyota",
Model: "prius"}
}
Run Code Online (Sandbox Code Playgroud)
但是,这给了我错误:不能在 NewCar 函数内的 car类型的结构文字中使用提升的字段 Car.Color,我该如何解决这个问题?我在网上阅读的所有内容都让我更加困惑。
Sha*_*ing 16
你需要:
func NewCar() *car {
return &car{ &A.Car{
Color: "red",
Make: "toyota",
Model: "prius",
}}
}
Run Code Online (Sandbox Code Playgroud)
请参阅https://github.com/golang/go/issues/9859了解:
建议:规范:直接引用结构文字中的嵌入字段
| 归档时间: |
|
| 查看次数: |
11793 次 |
| 最近记录: |