| 131 | } |
| 132 | |
| 133 | func ExampleForEachRow() { |
| 134 | conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) |
| 135 | if err != nil { |
| 136 | fmt.Printf("Unable to establish connection: %v", err) |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | rows, _ := conn.Query( |
| 141 | context.Background(), |
| 142 | "select n, n * 2 from generate_series(1, $1) n", |
| 143 | 3, |
| 144 | ) |
| 145 | var a, b int |
| 146 | _, err = pgx.ForEachRow(rows, []any{&a, &b}, func() error { |
| 147 | fmt.Printf("%v, %v\n", a, b) |
| 148 | return nil |
| 149 | }) |
| 150 | if err != nil { |
| 151 | fmt.Printf("ForEachRow error: %v", err) |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | // Output: |
| 156 | // 1, 2 |
| 157 | // 2, 4 |
| 158 | // 3, 6 |
| 159 | } |
| 160 | |
| 161 | func TestCollectRows(t *testing.T) { |
| 162 | defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { |