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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度