Go Web Frameworks interview question · Most asked
How should a Go service use database/sql correctly?
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
- How do you test HTTP handlers in Go?
- How do you implement graceful shutdown for a Go HTTP server?
- Compare Gin, Echo, Chi and Fiber.
- What is the http.Handler interface, and why is it central to Go web development?
- How do you pass request-scoped values (like the current user) through middleware in Go?
- Do you need a web framework in Go? What does Go 1.22's ServeMux provide?
- How do you design consistent error responses in a Go API?