英文:
Struct field not updated when implementing interface function
问题
如果我们有以下接口:
type IRoute interface {
AddChildren(child IRoute)
}
以下结构体:
type Route struct {
Alias string `json:"alias"`
Children []Route `json:"children,omitempty"`
Url string `json:"url,omitempty"`
}
并实现了该接口:
func (this Route) AddChildren(child globals.IRoute) {
this.Children = append(this.Children, child.(Route))
}
然后在我们的main
函数中,如果我们想要测试这个功能,它将无法正常工作:
rSettings := Route{"settings", nil, "/admin/settings"}
rSettingsContentTypesNew := models.Route{"new", nil, "/new?type&parent"}
rSettingsContentTypesEdit := models.Route{"edit", nil, "/edit/:nodeId"}
// 不起作用 - 没有添加子项
rSettingsContentTypes.AddChildren(rSettingsContentTypesNew)
rSettingsContentTypes.AddChildren(rSettingsContentTypesEdit)
rSettings.AddChildren(rSettingsContentTypes)
但是以下代码可以正常工作:
rSettings := Route{"settings", nil, "/admin/settings"}
rSettingsContentTypesNew := models.Route{"new", nil, "/new?type&parent"}
rSettingsContentTypesEdit := models.Route{"edit", nil, "/edit/:nodeId"}
// 这样确实可以正常工作
rSettingsContentTypes.Children = append(rSettingsContentTypes.Children, rSettingsContentTypesNew)
rSettingsContentTypes.Children = append(rSettingsContentTypes.Children, rSettingsContentTypesEdit)
rSettings.Children = append(rSettings.Children, rSettingsContentTypes)
我漏掉了什么?
英文:
If for example we have the following interface:
type IRoute interface {
AddChildren(child IRoute)
}
The following struct:
type Route struct {
Alias string `json:"alias"`
Children []Route `json:"children,omitempty"`
Url string `json:"url,omitempty"`
}
And implemented the interface:
func (this Route) AddChildren (child globals.IRoute){
this.Children = append(this.Children, child.(Route))
}
Then in our main func, if we wanted to test this it would not work:
rSettings := Route{"settings", nil, "/admin/settings"}
rSettingsContentTypesNew := models.Route{"new", nil, "/new?type&parent"}
rSettingsContentTypesEdit := models.Route{"edit", nil, "/edit/:nodeId"}
// Does NOT work - no children is added
rSettingsContentTypes.AddChildren(rSettingsContentTypesNew)
rSettingsContentTypes.AddChildren(rSettingsContentTypesEdit)
rSettings.AddChildren(rSettingsContentTypes)
And this does work as expected:
rSettings := Route{"settings", nil, "/admin/settings"}
rSettingsContentTypesNew := models.Route{"new", nil, "/new?type&parent"}
rSettingsContentTypesEdit := models.Route{"edit", nil, "/edit/:nodeId"}
// However this does indeed work
rSettingsContentTypes.Children = append(rSettingsContentTypes.Children,rSettingsContentTypesNew)
rSettingsContentTypes.Children = append(rSettingsContentTypes.Children,rSettingsContentTypesEdit)
rSettings.Children = append(rSettings.Children,rSettingsContentTypes)
What am I missing?
答案1
得分: 5
func (this Route) AddChildren (child globals.IRoute)
的接收者是一个值,所以你正在修改一个 Route
结构体的副本。
将其改为 func (this *Route) AddChildren (child globals.IRoute)
。
英文:
The receiver of func (this Route) AddChildren (child globals.IRoute)
is a value, so you are changing a copy of your Route
struct.
Change it to func (this *Route) AddChildren (child globals.IRoute)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论