dongtui8593 2019-07-30 00:28
浏览 56
已采纳

如何编辑埋在递归结构中的数组

I have this struct (notice that it is recursive!):

type Group struct {
  Name string
  Item []string
  Groups []Group
}

And I want to append a string to the Item array that is buried deep in the hierarchy of the Group array. The only information I have about the path of this new item is the names of the groups that it's in. Let's say the path is "foo/bar/far". I want to modify bar without overwriting foo, bar or the "root" array. Basically, I want to write a function that returns a new Group variable that is identical to the original variable but with the new string appended.

So far I've tried the following method:

Looping through an array that contains all the group names of the path and if they are in the current group set a current group variable to that new group. Once the loop has finished, append the string to the array and return current group. The only problem is, of course, that the rest of the root group is deleted and replaced with the new, modified group.

The code:

func in(item string, array []Group) (bool, int) {
        for i, elem := range array {
                if item == elem.Name {
                        return true, i
                } else {
                        continue
                }
        }

        return false, 0
}

func addItem(list Group, newItem string, path string) Group {
        var currentGroup Group = list
        if path == "" {
                currentGroup.Items = append(currentGroup.Items, newItem)
        } else {
                for _, elem := range strings.Split(path, "/") {
                        in, index := in(elem, currentGroup.Groups)
                        if in {
                                currentGroup = currentGroup.Groups[index]
                        }
                }
                currentGroup.Items = append(currentGroup.Items, newItem)
        }

        return currentGroup
}
  • 写回答

1条回答 默认 最新

  • dongzhi5846 2019-07-30 08:44
    关注

    I guess you could pass the group to addItem function as a pointer, and ignore the return value for the function

    A bit like

    func addItem(list *Group, newItem string, path string) Group {
        var currentGroup *Group = list
        if path == "" {
            currentGroup.Item = append(currentGroup.Item, newItem)
        } else {
            for _, elem := range strings.Split(path, "/") {
                in, index := in(elem, currentGroup.Groups)
                if in {
                    currentGroup = &currentGroup.Groups[index]
                }
            }
            currentGroup.Item = append(currentGroup.Item, newItem)
        }
    
        return *currentGroup
    }
    

    Complete example at: https://play.golang.org/p/_1BSF2LDQrE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致