实现接口函数时,结构字段未更新。

huangapple go评论76阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2015年2月13日 18:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/28497254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定