Account balance goes negative
Review this Go function that deducts from a wallet balance.
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
1func Withdraw(ctx context.Context, db *sql.DB, userID int64, amount int64) error {
2 var balance int64
3 err := db.QueryRowContext(ctx,
4 "SELECT balance FROM wallets WHERE user_id = $1", userID).Scan(&balance)
5 if err != nil {
6 return err
7 }
8 if balance < amount {
9 return ErrInsufficientFunds
10 }
11 _, err = db.ExecContext(ctx,
12 "UPDATE wallets SET balance = $1 WHERE user_id = $2", balance-amount, userID)
13 return err
14}
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.