为了账号安全,请及时绑定邮箱和手机立即绑定

实现接口功能时结构域未更新

实现接口功能时结构域未更新

Go
米脂 2021-09-13 16:35:04
例如,如果我们有以下接口: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))}然后在我们的主函数中,如果我们想测试它是行不通的: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 addedrSettingsContentTypes.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"}// However this does indeed workrSettingsContentTypes.Children = append(rSettingsContentTypes.Children,rSettingsContentTypesNew)rSettingsContentTypes.Children = append(rSettingsContentTypes.Children,rSettingsContentTypesEdit)rSettings.Children = append(rSettings.Children,rSettingsContentTypes)我错过了什么?:-)
查看完整描述

1 回答

?
慕虎7371278

TA贡献1802条经验 获得超4个赞

的接收者func (this Route) AddChildren (child globals.IRoute)是一个值,因此您正在更改Route结构的副本。

将其更改为 func (this *Route) AddChildren (child globals.IRoute)


查看完整回答
反对 回复 2021-09-13
  • 1 回答
  • 0 关注
  • 182 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信