Created
April 13, 2024 19:06
-
-
Save christoofar/363b5cfdf31f35dacf0a5ca332981027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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