Code Room
Code reviewMediumcr-g416
Subject Double checked lockingLevel Mid–Senior~25 minCommon in Concurrency · Code quality & review interviewsIndustries Software development

Question

Review this Go lazy initializer that replaces a hand-rolled double-checked lock with sync.Once.

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
var (	once sync.Once	db   *sql.DB) func DB() *sql.DB {	once.Do(func() {		conn, err := sql.Open("postgres", dsn)		if err != nil {			return		}		if err := conn.Ping(); err != nil {			return		}		db = conn	})	return db}
Run or narrate your approach, then ask the coach.