Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/connect/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ import (
"context"
"fmt"
"os"

"github.com/jackc/pgx/v5"
)

func main() {


ctx := context.Background()

// urlExample := "postgres://username:password@localhost:5432/schema_name"
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
conn, err := pgx.Connect(ctx, os.Getenv("CRATE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(ctx)
err = conn.Query(ctx, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3").Scan()
defer conn.Close(context.Background())
Copy link
Member

@amotl amotl Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't ctx be reused here, or must it NOT be used here intentionally? So, a new context.Background() is needed at this spot?

defer conn.Close(ctx)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably can


rows, err := conn.Query(ctx, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")
if err != nil {
fmt.Fprintf(os.Stderr, "Query failed: %v\n", err)
os.Exit(1)
Expand All @@ -53,7 +54,7 @@ func main() {
fmt.Fprintf(os.Stderr, "Scan failed: %v\n", err)
os.Exit(1)
}
fmt.Println(name, height)
fmt.Println(mountain, height)
}
if err := rows.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Rows error: %v\n", err)
Expand Down
Loading