You're developing a web application in Go, and you need to execute a SQL query that might return multiple rows. Which method from the database/sql package would you use, and how would you handle the returned rows?

  • Exec method; Execute the query and retrieve the result set directly. Use rows.Scan() within a loop to scan each row into variables for processing.
  • Prepare method; Prepare the SQL statement, execute it using Query or QueryRow, then scan each row using rows.Scan().
  • Query method; Use rows.Next() in a loop to fetch each row one by one, then process each row's data.
  • QueryRows method; Iterate over the rows using a loop and scan each row into a struct to process the data.
The correct method to use in this scenario is QueryRows. This method allows executing a SQL query that returns multiple rows. You can iterate over the rows using a loop and scan each row into a struct or variables for further processing.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *