Database connection nil return
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.
0:00 of about 25 min
Mark a line and say what kind of problem it is.0 findings
1var (
2 once sync.Once
3 db *sql.DB
4)
5
6func DB() *sql.DB {
7 once.Do(func() {
8 conn, err := sql.Open("postgres", dsn)
9 if err != nil {
10 return
11 }
12 if err := conn.Ping(); err != nil {
13 return
14 }
15 db = conn
16 })
17 return db
18}
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.