Skip to content

Instantly share code, notes, and snippets.

@christoofar
Created April 13, 2024 19:06
Show Gist options
  • Save christoofar/363b5cfdf31f35dacf0a5ca332981027 to your computer and use it in GitHub Desktop.
Save christoofar/363b5cfdf31f35dacf0a5ca332981027 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type content struct {
children []content
id int
}
// Recursive function to find content by ID
func findContentByID(node content, targetID int) *content {
if node.id == targetID {
return &node
}
for _, child := range node.children {
if found := findContentByID(child, targetID); found != nil {
return found
}
}
return nil
}
func main() {
// Example content tree
root := content{
id: 1,
children: []content{
{
id: 2,
children: []content{
{id: 3},
{id: 4},
},
},
{
id: 5,
children: []content{
{id: 6},
{id: 7},
},
},
},
}
// Find content with ID 4
targetID := 4
result := findContentByID(root, targetID)
if result != nil {
fmt.Printf("Content with ID %d found: %+v\n", targetID, *result)
} else {
fmt.Printf("Content with ID %d not found.\n", targetID)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment