下面的代碼無法設置或從基本實體獲取值如何使它能夠獲取基數以及繼承的結構來返回值type BaseEntity struct { Id string}func (p BaseEntity) SetId(Id string) { p.Id = Id}func (p BaseEntity) GetId() string { return p.Id}type Employee struct { BaseEntity Name string}type DataInterface interface { SetId(Id string) GetId() string }func getObjFactory() DataInterface { //return Data.Employee{BaseEntity: Data.BaseEntity{}} return new(Employee)}func main() { entity := getObjFactory() entity.SetId("TEST") fmt.Printf(">> %s", entity.GetId()) }
1 回答

白衣染霜花
TA貢獻1796條經驗 獲得超10個贊
該方法使用值接收器,因此是對象的副本,而不是指向該對象的指針。SetId
p
相反,您希望使用如下所示的指針接收器:
func (p *BaseEntity) SetId(Id string) { p.Id = Id }
您可以在“Go 之旅”中的“選擇值或指針接收器”部分下找到更多詳細信息。
- 1 回答
- 0 關注
- 99 瀏覽
添加回答
舉報
0/150
提交
取消