diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-05 08:51:17 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-05 08:51:17 +0800 |
commit | 7c341685f878aa8fd4c49788cf8cc0d8c5c6e127 (patch) | |
tree | 5295f1f4020e85c065e98fcb88ec217ad66e078a /database.go | |
parent | config: Add explanatory comments (diff) | |
download | forge-7c341685f878aa8fd4c49788cf8cc0d8c5c6e127.tar.gz forge-7c341685f878aa8fd4c49788cf8cc0d8c5c6e127.tar.zst forge-7c341685f878aa8fd4c49788cf8cc0d8c5c6e127.zip |
*: Replace some := with var
Diffstat (limited to 'database.go')
-rw-r--r-- | database.go | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/database.go b/database.go index 14e19c0..0459fb7 100644 --- a/database.go +++ b/database.go @@ -5,27 +5,29 @@ package main import ( "context" + + "github.com/jackc/pgx/v5" ) // query_list is a helper function that executes a query and returns a list of // results. -func query_list[T any](ctx context.Context, query string, args ...any) ([]T, error) { - rows, err := database.Query(ctx, query, args...) - if err != nil { +func query_list[T any](ctx context.Context, query string, args ...any) (result []T, err error) { + var rows pgx.Rows + + if rows, err = database.Query(ctx, query, args...); err != nil { return nil, err } defer rows.Close() - var result []T for rows.Next() { var item T - if err := rows.Scan(&item); err != nil { + if err = rows.Scan(&item); err != nil { return nil, err } result = append(result, item) } - if err := rows.Err(); err != nil { + if err = rows.Err(); err != nil { return nil, err } @@ -34,17 +36,17 @@ func query_list[T any](ctx context.Context, query string, args ...any) ([]T, err // query_name_desc_list is a helper function that executes a query and returns a // list of name_desc_t results. -func query_name_desc_list(ctx context.Context, query string, args ...any) ([]name_desc_t, error) { - rows, err := database.Query(ctx, query, args...) - if err != nil { +func query_name_desc_list(ctx context.Context, query string, args ...any) (result []name_desc_t, err error) { + var rows pgx.Rows + + if rows, err = database.Query(ctx, query, args...); err != nil { return nil, err } defer rows.Close() - result := []name_desc_t{} for rows.Next() { var name, description string - if err := rows.Scan(&name, &description); err != nil { + if err = rows.Scan(&name, &description); err != nil { return nil, err } result = append(result, name_desc_t{name, description}) |