Control Go return types using sqlc query annotations
mainThe sqlc tool determines the return type of the generated Go methods based on the suffix annotation used in your SQL statements within queries.sql. Use these suffixes to control what the generated method returns:
| Annotation | Return Type (Go) | Description |
|---|---|---|
:exec | error | Indicates only whether the query succeeded. |
:execrows | (int64, error) | Returns the number of rows affected and an error. |
:one | (Struct, error) | Returns a single struct instance and an error. |
:many | ([]Struct, error) | Returns a slice of structs and an error. |
Example of how annotations affect the generated signature:
:one$\rightarrow$(Author, error):many$\rightarrow$([]Author, error)
-- Example usage in queries.sql
-- Get a single author
-- name: GetAuthor :one
SELECT * FROM authors WHERE id = $1 LIMIT 1;
-- Get many authors
-- name: ListAuthors :many
SELECT * FROM authors;