Code RoomClose error overwrites write error
HardPrep Room Coding #2121

Close error overwrites write error

Code reviewCode quality & reviewSenior–Staff~18 min

Review this Go function that writes a record to a file and closes it in a deferred call.

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 writeRecord(path string, rec []byte) (err error) {
2 f, err := os.Create(path)
3 if err != nil {
4 return err
5 }
6 defer func() {
7 err = f.Close()
8 }()
9 if _, err := f.Write(rec); err != nil {
10 return err
11 }
12 return nil
13}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.