Code Room
Code reviewHard
Question
Review this Go signup function that enforces one account per email.
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.
Learn the concepts
func CreateUser(ctx context.Context, db *sql.DB, email, name string) error { tx, _ := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted}) var n int tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM users WHERE email = $1", email).Scan(&n) if n > 0 { tx.Rollback() return ErrEmailTaken } _, err := tx.ExecContext(ctx, "INSERT INTO users (email, name) VALUES ($1, $2)", email, name) if err != nil { tx.Rollback() return err } return tx.Commit()}Run or narrate your approach, then ask the coach.