Code Room
Code reviewHardcr-g222
Subject Thread safetyLevel Senior–Staff~40 minCommon in Concurrency · Code quality & review interviewsIndustries Software development

Question

Review this Go lazy-connection initializer.

What is the bug with this sync.Once usage?

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.

Talk through your review
Code to reviewgo
type DB struct {    once sync.Once    conn *sql.DB} func (d *DB) Conn() (*sql.DB, error) {    var err error    d.once.Do(func() {        d.conn, err = sql.Open("postgres", dsn())        if err == nil {            err = d.conn.Ping()      // verify connectivity        }    })    return d.conn, err}
Run or narrate your approach, then ask the coach.