英文:
Retrieve a slice of IDs recursively in GO
问题
type MenuItem struct {
Id int32
Type string
SortOrder int8
CategoryId int32
Children []*MenuItem
}
func getCategoryIds(menuItems []*MenuItem) []int32 {
var categoryIds []int32
for _, menuItem := range menuItems {
categoryIds = append(categoryIds, menuItem.CategoryId)
if len(menuItem.Children) > 0 {
categoryIds = append(categoryIds, getCategoryIds(menuItem.Children)...)
}
}
return categoryIds
}
func main() {
menuItems := []MenuItem{
{
Id: 1,
Type: "type1",
SortOrder: 1,
CategoryId: 11,
Children: []*MenuItem{
{
Id: 2,
Type: "type2",
SortOrder: 2,
CategoryId: 12,
Children: []*MenuItem{
{
Id: 3,
Type: "type3",
SortOrder: 3,
CategoryId: 13,
Children: nil,
},
},
},
{
Id: 4,
Type: "type4",
SortOrder: 4,
CategoryId: 14,
Children: []*MenuItem{
{
Id: 5,
Type: "type5",
SortOrder: 5,
CategoryId: 15,
Children: nil,
},
},
},
},
},
{
Id: 6,
Type: "type6",
SortOrder: 6,
CategoryId: 16,
Children: []*MenuItem{
{
Id: 7,
Type: "type7",
SortOrder: 7,
CategoryId: 17,
Children: []*MenuItem{
{
Id: 8,
Type: "type8",
SortOrder: 8,
CategoryId: 18,
Children: nil,
},
},
},
{
Id: 9,
Type: "type9",
SortOrder: 9,
CategoryId: 19,
Children: []*MenuItem{
{
Id: 10,
Type: "type10",
SortOrder: 10,
CategoryId: 20,
Children: nil,
},
},
},
},
},
}
categoryIds := getCategoryIds(menuItems)
fmt.Println(categoryIds)
}
这段代码定义了一个 MenuItem
结构体,其中包含了 Id
、Type
、SortOrder
、CategoryId
和 Children
字段。为了获取仅包含 CategoryId
值的切片,我们可以使用递归的方式来处理 Children
字段。通过遍历 menuItems
切片,将每个 MenuItem
的 CategoryId
值添加到 categoryIds
切片中,并且如果该 MenuItem
的 Children
不为空,则递归调用 getCategoryIds
函数来处理子菜单项。最后,返回 categoryIds
切片。
在 main
函数中,我们创建了一个示例的 menuItems
切片,并调用 getCategoryIds
函数来获取仅包含 CategoryId
值的切片。最后,将结果打印输出。
英文:
type MenuItem struct {
Id int32
Type string
SortOrder int8
CategoryId int32
Children []*MenuItem
}
If you have this struct, and you have a slice of []MenuItem
. I would like to retrieve a slice only with CategoryId
values. This looks like a recursive use case due to the Children
but based on how append function for slices works, it looks a bit tricky. Any ideas? How would you do it?
The end result should be a slice of []int32
: [11, 1900, 12, 1300,...., 2090]
答案1
得分: 3
让append
为您完成繁重的工作,处理内存扩展,并在每个递归步骤中返回其结果:
func traverse(m *MenuItem) (r []int32) {
if m == nil {
return
}
r = append(r, m.CategoryId)
for _, c := range m.Children {
r = append(r, traverse(c)...)
}
return
}
链接:https://go.dev/play/p/8d0cyPMV0r6
英文:
Let append
do the hard work for you, handling memory expansion, and return its result during each recursive step:
func traverse(m *MenuItem) (r []int32) {
if m == nil {
return
}
r = append(r, m.CategoryId)
for _, c := range m.Children {
r = append(r, traverse(c)...)
}
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论