实现接口函数时,Struct字段不更新 [英] Struct field not updated when implementing interface function

查看:80
本文介绍了实现接口函数时,Struct字段不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有以下接口:

  type IRoute interface {
AddChildren(child IRoute)$


$ b $ p
$ b

以下结构:


< $ b $ p> 类型路由结构{
别名字符串`json:别名``
Children [] Route`json:children,omitempty`
Url字符串`json:url,omitempty`
}

并实现了接口:

  func(This Route)AddChildren(child globals.IRoute){
this.Children = append(this.Children, (Route))
}

然后在我们的主要函数中,如果我们想要测试这是行不通的:

  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)


$
$ b

  rSettings:= Route {settings,nil,/ b $ b 

admin / settings}
rSettingsContentTypesNew:= models.Route {new,nil,/ new?type& parent}
rSettingsContentTypesEdit:= models.Route {edit,nil,/编辑/:nodeId}

//然而,这确实有效
rSettingsContentTypes.Children = append(rSettingsContentTypes.Children,rSettingsContentTypesNew)
rSettingsContentTypes.Children = append(rSettingsContentTypes.Children ,rSettingsContentTypesEdit)
rSettings.Children = append(rSettings.Children,rSettingsContentTypes)

什么是我错过了? : 是一个值,因此您正在更改 Route 结构的副本。



更改它到 func(this * Route)AddChildren(child globals.IRoute)


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? :-)

解决方案

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)

这篇关于实现接口函数时,Struct字段不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆