Go Web Frameworks interview question · Most asked

How should a Go service use database/sql correctly?

Create a free account to save this page, take notes and track your progress. Sign in

Summary

Open one *sql.DB (a concurrency-safe pool) at startup and share it, configure pool limits, always use placeholders, pass contexts to every query, close rows, check rows.Err(), and translate sql.ErrNoRows to a domain 'not found' error.

Longer Answer

Use transactions (BeginTx, defer tx.Rollback(), Commit) for multi-step changes, and conditional updates or row locks to prevent check-then-act races. Libraries such as sqlx, sqlc or GORM sit on top; hide them behind a repository interface.

Key Points

  • One pool, shared.
  • Placeholders and contexts.
  • Close rows; handle ErrNoRows.
  • Transactions for multi-step changes.

Deep dive: Go Web Frameworks: Database Access

Explanation

What interviewers look for

Pool awareness, injection safety, context use and transaction handling.

More most-asked Go Web Frameworks questions

← All Go Web Frameworks questions

Was this page helpful?