How to Scan Returning Struct with github.com/jmoiron/sqlx
Автор: vlogize
Загружено: 2025-04-05
Просмотров: 5
Learn how to effectively use sqlx to scan SQL return values into a struct in Go with a practical example.
---
This video is based on the question https://stackoverflow.com/q/77936293/ asked by the user 'dmbibishkin' ( https://stackoverflow.com/u/23181980/ ) and on the answer https://stackoverflow.com/a/77938778/ provided by the user 'ABDULLOKH MUKHAMMADJONOV' ( https://stackoverflow.com/u/10303199/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to scan returning struct with github.com/jmoiron/sqlx
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Scan Returning Struct with github.com/jmoiron/sqlx
When working with databases in Go, you might encounter situations where you need to not just insert data but also retrieve some values back, particularly when using an SQL command that includes a RETURNING clause. One popular package that helps with SQL interactions in Go is sqlx by jmoiron.
In this post, we'll tackle a common issue: how to scan SQL returning values into a struct using the sqlx package. This is an essential skill for any Go developer working with database operations.
The Problem
You might find yourself writing code like this:
[[See Video to Reveal this Text or Code Snippet]]
You define a User struct to hold the values:
[[See Video to Reveal this Text or Code Snippet]]
Then, you try to execute a query with db.QueryRowx(CreateUserSQL, user).StructScan(user). However, you encounter an error:
[[See Video to Reveal this Text or Code Snippet]]
This error indicates that QueryRowx can't handle named arguments directly, which leaves many developers puzzled.
The Solution
Fortunately, there's a workaround! Instead of using QueryRowx, you can use a combination of NamedQuery and StructScan. Let's break it down step by step.
Step 1: Update Your SQL Statement
Make sure your SQL command's RETURNING clause does not use parentheses:
[[See Video to Reveal this Text or Code Snippet]]
Step 2: Create the User Struct
You can create the User struct as usual:
[[See Video to Reveal this Text or Code Snippet]]
Step 3: Use NamedQuery
Instead of QueryRowx, you will do the following:
[[See Video to Reveal this Text or Code Snippet]]
Key Points to Remember
Always use NamedQuery when you need to insert values using named parameters and also retrieve values back into a struct.
Avoid using a pointer for the struct when calling StructScan — pass the struct by value.
If you're encountering issues, always check for syntax errors in your SQL queries.
Conclusion
By following these steps, you can effectively scan returning values from your SQL commands into a struct using sqlx. This approach not only solves the problem but also helps you maintain clean and readable code.
If you have any questions or additional tips about working with sqlx, feel free to leave a comment below!

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: