Skip to content

Commit 9767399

Browse files
committed
Go: Add minimal example
1 parent ced0b32 commit 9767399

11 files changed

Lines changed: 348 additions & 0 deletions

File tree

.github/dependabot.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ updates:
4848
schedule:
4949
interval: "daily"
5050

51+
- directory: "/by-language/go-pgx"
52+
package-ecosystem: "gomod"
53+
schedule:
54+
interval: "daily"
55+
5156
- directory: "/by-language/java-jdbc"
5257
package-ecosystem: "maven"
5358
schedule:

.github/workflows/lang-go-pgx.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Go pgx
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '.github/workflows/lang-go-pgx.yml'
7+
- 'by-language/go-pgx/**'
8+
- '/requirements.txt'
9+
push:
10+
branches: [ main ]
11+
paths:
12+
- '.github/workflows/lang-go-pgx.yml'
13+
- 'by-language/go-pgx/**'
14+
- '/requirements.txt'
15+
16+
# Allow job to be triggered manually.
17+
workflow_dispatch:
18+
19+
# Run job each night after CrateDB nightly has been published.
20+
schedule:
21+
- cron: '0 3 * * *'
22+
23+
# Cancel in-progress jobs when pushing to the same branch.
24+
concurrency:
25+
cancel-in-progress: true
26+
group: ${{ github.workflow }}-${{ github.ref }}
27+
28+
jobs:
29+
test:
30+
name: "
31+
Go: ${{ matrix.go-version }}
32+
CrateDB: ${{ matrix.cratedb-version }}
33+
on ${{ matrix.os }}"
34+
runs-on: ${{ matrix.os }}
35+
strategy:
36+
fail-fast: false
37+
matrix:
38+
os: [ 'ubuntu-latest' ]
39+
cratedb-version: [ 'nightly' ]
40+
go-version: [
41+
'1.23.x',
42+
'1.24.x',
43+
]
44+
45+
services:
46+
cratedb:
47+
image: crate/crate:${{ matrix.cratedb-version }}
48+
ports:
49+
- 4200:4200
50+
- 5432:5432
51+
env:
52+
CRATE_HEAP_SIZE: 4g
53+
54+
steps:
55+
56+
- name: Acquire sources
57+
uses: actions/checkout@v4
58+
59+
- name: Install Go
60+
uses: actions/setup-go@v5
61+
with:
62+
go-version: ${{ matrix.go-version }}
63+
64+
- name: Install utilities
65+
run: |
66+
pip install -r requirements.txt
67+
68+
- name: Validate by-language/go-pgx
69+
run: |
70+
ngr test by-language/go-pgx

by-language/go-pgx/DEVELOP.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Maintenance
2+
3+
A few project maintenance commands.
4+
5+
Invoke linter.
6+
```shell
7+
gofmt -l -d .
8+
```
9+
10+
Format code.
11+
```shell
12+
gofmt -w .
13+
```
14+
15+
Display available minor and patch upgrades for all direct and indirect dependencies.
16+
```shell
17+
go list -u -m all
18+
```
19+
20+
Update to the latest patch releases, including test dependencies.
21+
```shell
22+
go get -u=patch -t
23+
go mod tidy
24+
```

by-language/go-pgx/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Connecting to CrateDB with Go
2+
3+
## About
4+
5+
The files `test/basic_queries.go` and `test/bulk_operations.go` include
6+
basic example programs that use the canonical PostgreSQL driver for Go,
7+
[pgx], to connect to CrateDB.
8+
9+
## Usage
10+
11+
Start a CrateDB instance for evaluation purposes.
12+
```shell
13+
docker run -it --rm --publish=4200:4200 --publish=5432:5432 \
14+
crate:latest -Cdiscovery.type=single-node
15+
```
16+
17+
Invoke example program.
18+
```shell
19+
go run .
20+
```
21+
22+
Invoke test cases.
23+
```shell
24+
go test -v
25+
```
26+
27+
28+
[pgx]: https://github.com/jackc/pgx
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/jackc/pgx/v5"
10+
)
11+
12+
func runBasicQueries(connStr string) {
13+
14+
fmt.Println("# runBasicQueries")
15+
16+
ctx := context.Background()
17+
conn, err := pgx.Connect(ctx, connStr)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
defer conn.Close(ctx)
22+
var name string
23+
err = conn.QueryRow(ctx, "select name || $1 from sys.cluster", "foo").Scan(&name)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
fmt.Println(name)
28+
commandTag, err := conn.Exec(ctx, "create table if not exists t1 (x integer, ts timestamp)")
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
fmt.Println(commandTag)
33+
ts := time.Now()
34+
// Convert to UTC
35+
loc, _ := time.LoadLocation("UTC")
36+
ts = ts.In(loc)
37+
commandTag, err = conn.Exec(ctx, "insert into t1 (x, ts) values (?, ?)", 1, ts)
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
fmt.Println(commandTag)
42+
commandTag, err = conn.Exec(ctx, "refresh table t1")
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
fmt.Println(commandTag)
47+
var tsRead time.Time
48+
err = conn.QueryRow(ctx, "select ts from t1").Scan(&tsRead)
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
if tsRead.Sub(ts) > (1 * time.Second) {
53+
log.Fatal("Inserted ts doesn't match read ts: ", ts, tsRead)
54+
}
55+
commandTag, err = conn.Exec(ctx, "update t1 set x = ?", 2)
56+
if err != nil {
57+
log.Fatal(err)
58+
}
59+
fmt.Println(commandTag)
60+
commandTag, err = conn.Exec(ctx, "refresh table t1")
61+
if err != nil {
62+
log.Fatal(err)
63+
}
64+
fmt.Println(commandTag)
65+
commandTag, err = conn.Exec(ctx, "delete from t1 where x = ?", 2)
66+
if err != nil {
67+
log.Fatal(err)
68+
}
69+
fmt.Println(commandTag)
70+
71+
fmt.Println()
72+
73+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"math/rand"
8+
"os"
9+
"strconv"
10+
11+
"github.com/jackc/pgx/v5"
12+
)
13+
14+
func submitDdl(ctx context.Context, conn *pgx.Conn, ddl string) error {
15+
16+
sql, err := os.ReadFile("setup.sql")
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
21+
commandTag, err := conn.Exec(ctx, string(sql))
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
fmt.Println(commandTag)
26+
return nil
27+
}
28+
29+
func runBulkOperations(connStr string) {
30+
31+
fmt.Println("# runBulkOperations")
32+
33+
num_batches := 20
34+
batch_size := 500
35+
36+
ctx := context.Background()
37+
conn, err := pgx.Connect(ctx, connStr)
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
42+
err = submitDdl(ctx, conn, "foo")
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
47+
_, err = conn.Prepare(ctx, "ps1", "INSERT INTO go_users (id, name, value) VALUES ($1, $2, $3)")
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
52+
for b := 0; b < num_batches; b++ {
53+
fmt.Println("batch " + strconv.Itoa(b))
54+
55+
batch := &pgx.Batch{}
56+
for x := 0; x < batch_size; x++ {
57+
id := b*x + x
58+
username := "user_" + strconv.Itoa(b) + "_" + strconv.Itoa(x)
59+
batch.Queue("ps1", id, username, rand.Float32())
60+
}
61+
62+
br := conn.SendBatch(ctx, batch)
63+
_, err := br.Exec()
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
err = br.Close()
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
}
72+
73+
fmt.Println()
74+
75+
}

by-language/go-pgx/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/cratedb-examples/by-language/go-pgx
2+
3+
go 1.23
4+
5+
toolchain go1.24.1
6+
7+
require github.com/jackc/pgx/v5 v5.7.2
8+
9+
require (
10+
github.com/jackc/pgpassfile v1.0.0 // indirect
11+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
12+
golang.org/x/crypto v0.31.0 // indirect
13+
golang.org/x/text v0.21.0 // indirect
14+
)

by-language/go-pgx/go.sum

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
5+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
6+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
7+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
8+
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
9+
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
10+
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
11+
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
12+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
13+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
15+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
16+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
18+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
19+
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
20+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
21+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
22+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
23+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
24+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
25+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
27+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
28+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

by-language/go-pgx/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
hosts := flag.String("hosts", "localhost", "CrateDB hostname")
10+
port := flag.Int("port", 5432, "CrateDB postgres port")
11+
flag.Parse()
12+
connStr := fmt.Sprintf("postgres://crate@%s:%d/testdrive", *hosts, *port)
13+
runBasicQueries(connStr)
14+
runBulkOperations(connStr)
15+
}

by-language/go-pgx/main_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAll(t *testing.T) {
8+
main()
9+
}

0 commit comments

Comments
 (0)