build-a-video-container-parser / lesson-12.md
Lesson 12 · The box tree

Finding a box by path

Now that you have a tree, you need a way to reach into it. Today you look up a box by a slash-separated path like moov/trak/mdia/hdlr, which is how every later lesson will grab the exact box it needs.

The goal

Find a box in the tree by a slash-separated type path, returning nil when it is absent.

Start here - the target
TO DO
Scenario: A path locates a nested box, or nothing
Giventhe tree from the moov/trak/mdia/hdlr buffer
Whenfind is called with the path "moov/trak/mdia/hdlr"
Thenit returns the hdlr box
Andfind with the path "moov/trak/tkhd" returns nil because no tkhd is present
Background

With a tree in hand, navigation should read like a filesystem path. find takes a slash-separated list of types and walks down: match the first segment against the boxes at this level, descend into that box’s children, and continue with the rest of the path. When the last segment matches, you have your box; when any segment has no match, you return nil.

This single helper is what keeps every later lesson short. Instead of manually threading through moov, then trak, then mdia, a lesson just asks for find(tree, "moov/trak/mdia/mdhd") and parses the box it gets back. It also closes the box-tree chapter with something you can genuinely use: give it a tree and a path, and it hands you the exact box.

Make it work
// walk one path segment at a time, descending into children
func find(boxes []Box, path string) *Box {
parts := strings.Split(path, "/")
// at each level, pick the child matching parts[0], then recurse
// on its children with the rest of the path; return nil if missing
}
CheckpointDONE
You can find a box by path. Commit and stop here.