
database/sql
database/sql, connections, queries, prepared statements, transactions, connection pooling
1What is the main difference between DB.Query and DB.Exec?
What is the main difference between DB.Query and DB.Exec?
Відповідь
DB.Query is used to execute SELECT queries that return rows of data, while DB.Exec is for executing SQL commands that don't return results (INSERT, UPDATE, DELETE). Query returns a Rows object that must be iterated and closed with rows.Close(), whereas Exec returns a Result containing the number of affected rows and the last inserted ID.
2Why is it important to close the Rows object after a query?
Why is it important to close the Rows object after a query?
Відповідь
Closing Rows with rows.Close() releases the database connection resources, allowing the connection to return to the pool. Without closing, connections remain occupied and the pool can be exhausted, causing blockages. It's recommended to use defer rows.Close() immediately after checking the Query error to guarantee closure even if a subsequent error occurs.
3Which method should be used to retrieve a single row of results?
Which method should be used to retrieve a single row of results?
Відповідь
DB.QueryRow is specifically designed to retrieve a single row and returns a Row object (singular). Unlike Query which returns Rows and requires iteration with rows.Next(), QueryRow simplifies the code by allowing direct scanning. Error handling occurs during Scan, where sql.ErrNoRows indicates that no row was found.
How to open a connection to a database with database/sql?
What does the rows.Scan() method return?
+19 питань зі співбесід
Інші теми співбесід Go
Go Basics
Go Data Structures
Go Interfaces
Error Handling
Goroutines Basics
Channels
Go Modules
HTTP Server
HTTP Client
JSON Encoding
Context Package
Testing
Concurrency Patterns
Sync Primitives
Go Web Frameworks
REST API Design
gRPC
Reflection
Memory Management
Performance Optimization
Generics
Go Design Patterns
Microservices
Security & Authentication
Docker & Containerization
Kubernetes Basics
Advanced Go
CLI Development
Опануй Go для наступної співбесіди
Отримай доступ до всіх питань, flashcards, технічних тестів, вправ code review та симуляторів співбесід.
Почни безкоштовно