递归函数在Go中没有保存更改。

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

Recursive function in go is not storing changes

问题

//递归设置Component结构中所有Parts、PartUse和Components的ExternalID
func (svc *ProductManagementClient) setExternalID(input *[]Component) *[]Component {
for _, component := range *input {
if component.Components != nil {
component.Components = svc.setExternalID(component.Components)
}
component.PartUse.ExternalID = component.PartUse.ID
component.Part.ExternalID = component.Part.ID
for _, occurrence := range component.Occurrences {
occurrence.ExternalID = occurrence.ID
}
component.ExternalID = "PartID:" + component.Part.ID + ",PartUseID:" + component.PartUse.ID
zap.S().Debug("组件的ExternalID为:", component.ExternalID)
}
return input
}

在这个函数中,我试图为这些结构设置ExternalID字段,并且我从另一个函数中调用它。下面是那段代码的片段:

//为每个Part、PartUse和Component设置externalID
for _, component := range retBOM.Components {
component.Components = svc.setExternalID(component.Components)
}

更改没有被持久化,我无法确定原因。当我查看结果时,ExternalID字段仍然为空。我正在编写一个递归函数,因为Component字段是嵌套的。我该如何修复这个问题?

我尝试通过引用传递,但显然在Golang中不允许这样做。

英文:
//Recursively set ExternalID for all Parts, PartUse, Components in Component struct
func (svc *ProductManagementClient) setExternalID(input *[]Component) *[]Component {
	for _, component := range *input {
		if component.Components != nil {
			component.Components = svc.setExternalID(component.Components)
		}
		component.PartUse.ExternalID = component.PartUse.ID
		component.Part.ExternalID = component.Part.ID
		for _, occurrence := range component.Occurrences {
			occurrence.ExternalID = occurrence.ID
		}
		component.ExternalID = "PartID:" + component.Part.ID + ",PartUseID:" + component.PartUse.ID
		zap.S().Debug("ExternalID for component:", component.ExternalID)
	}
	return input
}

In this function, I'm trying to set the ExternalID field for these structs, and I'm calling it from another function. The code snippet for that is below:

// Set externalID for each Part, PartUse and Component
for _, component := range retBOM.Components {
	component.Components = svc.setExternalID(component.Components)
}

The changes aren't being persisted, and I'm unable to tell why. When I look at the result, the ExternalID fields are still coming up empty. I'm writing a recursive function because the Component field is nested. How can I fix this?

I tried to pass by reference but apparently that's not allowed in Golang.

答案1

得分: 0

因为在循环中,你修改的是 Component 的 copy。在迭代结束时,你必须替换它,像这样:

for i, component := range *input {
    if component.Components != nil {
        component.Components = svc.setExternalID(component.Components)
    }
    component.PartUse.ExternalID = component.PartUse.ID
    component.Part.ExternalID = component.Part.ID
    for _, occurrence := range component.Occurrences {
        occurrence.ExternalID = occurrence.ID
    }
    component.ExternalID = "PartID:" + component.Part.ID + ",PartUseID:" + component.PartUse.ID
    zap.S().Debug("ExternalID for component:", component.ExternalID)
    // replace current state by new one
    *input[i] = component
}

或者使用 []*Component / *[]*Component 替代 *[]Component。

英文:

Its because in cycle youre modifying copy of Component. at the end of iteration you must replace it like

for i, component := range *input {
    if component.Components != nil {
        component.Components = svc.setExternalID(component.Components)
    }
    component.PartUse.ExternalID = component.PartUse.ID
    component.Part.ExternalID = component.Part.ID
    for _, occurrence := range component.Occurrences {
        occurrence.ExternalID = occurrence.ID
    }
    component.ExternalID = "PartID:" + component.Part.ID + ",PartUseID:" + component.PartUse.ID
    zap.S().Debug("ExternalID for component:", component.ExternalID)
    // replace current state by new one
    *input[i] = component
}

or use []*Component / *[]*Component instead of *[]Component

答案2

得分: 0

这里有一个相关的问题链接

但是正如许多人指出的那样,你传递的是组件的副本而不是切片的地址。

所以处理这个问题的最快方法是使用[]*Component,这在通俗的说法中意味着一个指向组件的切片。

英文:

A closely related question here

But as many pointed out, you are passing the copy of the component but address of the slice.

so the fastest way to handle this is to use
[]*Component which in layman means a slice of pointer components

huangapple
  • 本文由 发表于 2022年8月17日 22:05:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/73389914.html
匿名

发表评论

匿名网友

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

确定