Skip to content

Commit 58488ca

Browse files
committed
Connect: Add Go
1 parent c2721f4 commit 58488ca

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

docs/connect/go/index.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
(connect-go)=
2+
3+
# Go
4+
5+
:::{include} /_include/links.md
6+
:::
7+
8+
:::{div} sd-text-muted
9+
Use pgx to connect to CrateDB from Go applications.
10+
:::
11+
12+
(go-pgx)=
13+
## pgx
14+
15+
:::{rubric} About
16+
:::
17+
18+
[pgx] is a pure Go driver and toolkit for PostgreSQL.
19+
20+
:::{rubric} Synopsis
21+
:::
22+
23+
`go.mod`
24+
```text
25+
module github.com/cratedb-guide/connect/go/pgx
26+
require github.com/jackc/pgx/v5 v5.7.6
27+
```
28+
`example.go`
29+
```go
30+
package main
31+
32+
import (
33+
"context"
34+
"fmt"
35+
36+
"github.com/jackc/pgx/v5"
37+
)
38+
39+
func main() {
40+
ctx := context.Background()
41+
42+
// Connect to database.
43+
conn, _ := pgx.Connect(ctx, "postgresql://crate:crate@localhost:5432/doc?sslmode=disable")
44+
defer conn.Close(ctx)
45+
46+
// Invoke basic query.
47+
rows, _ := conn.Query(ctx, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")
48+
defer rows.Close(ctx)
49+
50+
// Display results.
51+
for rows.Next() {
52+
var mountain string
53+
var height int
54+
rows.Scan(&mountain, &height)
55+
fmt.Println(mountain, height)
56+
}
57+
}
58+
```
59+
60+
:::{include} ../_cratedb.md
61+
:::
62+
```shell
63+
go mod tidy
64+
go run example.go
65+
```
66+
67+
:::{rubric} CrateDB Cloud
68+
:::
69+
70+
For connecting to CrateDB Cloud, use `sslmode=require`, and
71+
replace username, password, and hostname with values matching
72+
your environment.
73+
```go
74+
conn, _ := pgx.Connect(ctx, "postgresql://admin:password@testcluster.cratedb.net:5432/doc?sslmode=require")
75+
```
76+
77+
:::{rubric} Example
78+
:::
79+
80+
:::{card}
81+
:link: https://github.com/crate/cratedb-examples/tree/main/by-language/go-pgx
82+
:link-type: url
83+
{material-outlined}`play_arrow;2em`
84+
Connect to CrateDB and CrateDB Cloud using Go.
85+
+++
86+
Demonstrates basic examples and bulk insert operations using the pgx driver.
87+
:::
88+
89+
[![Go pgx CI](https://github.com/crate/cratedb-examples/actions/workflows/lang-go-pgx.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-go-pgx.yml)
90+
91+
92+
(go-pq)=
93+
## pq
94+
95+
:::{rubric} About
96+
:::
97+
98+
[pq] is a pure Go PostgreSQL driver for Go's database/sql package.
99+
100+
:::{rubric} Synopsis
101+
:::
102+
103+
`go.mod`
104+
```text
105+
module github.com/cratedb-guide/connect/go/pq
106+
require github.com/lib/pq v1.10.9
107+
```
108+
`example.go`
109+
```go
110+
package main
111+
112+
import (
113+
"database/sql"
114+
"fmt"
115+
116+
_ "github.com/lib/pq"
117+
)
118+
119+
func main() {
120+
121+
// Connect to database.
122+
connStr := "postgresql://crate:crate@localhost:5432/doc?sslmode=disable"
123+
db, _ := sql.Open("postgres", connStr)
124+
defer db.Close()
125+
126+
// Invoke basic query.
127+
rows, _ := db.Query("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")
128+
defer rows.Close()
129+
130+
// Display results.
131+
for rows.Next() {
132+
var mountain string
133+
var height int
134+
rows.Scan(&mountain, &height)
135+
fmt.Println(mountain, height)
136+
}
137+
}
138+
```
139+
140+
:::{include} ../_cratedb.md
141+
:::
142+
```shell
143+
go mod tidy
144+
go run example.go
145+
```
146+
147+
:::{rubric} CrateDB Cloud
148+
:::
149+
150+
For connecting to CrateDB Cloud, use `sslmode=require`, and
151+
replace username, password, and hostname with values matching
152+
your environment.
153+
```go
154+
connStr := "postgresql://admin:password@testcluster.cratedb.net:5432/doc?sslmode=require"
155+
```
156+
157+
158+
[pgx]: https://github.com/jackc/pgx
159+
[pq]: https://github.com/lib/pq

docs/connect/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
4949
:margin: 4 4 0 0
5050
:padding: 0
5151

52+
::::{grid-item-card} Go
53+
:link: connect-go
54+
:link-type: ref
55+
:link-alt: Connect to CrateDB using Go
56+
:padding: 3
57+
:text-align: center
58+
:class-card: sd-pt-3
59+
:class-body: sd-fs-1
60+
:class-title: sd-fs-6
61+
{fab}`golang`
62+
::::
63+
5264
::::{grid-item-card} Java
5365
:link: connect-java
5466
:link-type: ref
@@ -182,6 +194,7 @@ application
182194
:maxdepth: 1
183195
:hidden:
184196
197+
go/index
185198
java
186199
javascript
187200
php

0 commit comments

Comments
 (0)