Walk leaks file descriptors
Review this Go function that finds the largest file under a directory tree.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 18 min
Mark a line and say what kind of problem it is.0 findings
1func largestFile(root string) (string, int64, error) {
2 var bestPath string
3 var bestSize int64
4 err := filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
5 if err != nil || info.IsDir() {
6 return err
7 }
8 f, err := os.Open(p)
9 if err != nil {
10 return err
11 }
12 st, _ := f.Stat()
13 if st.Size() > bestSize {
14 bestSize = st.Size()
15 bestPath = p
16 }
17 return nil
18 })
19 return bestPath, bestSize, err
20}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.