A Go (Golang) client for Postgres Message Queue (PGMQ). Based loosely on the Rust client.
Start a Postgres instance with the PGMQ extension installed:
docker run -d --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 quay.io/tembo/pgmq-pg:latest
Then
package main
import (
"context"
"fmt"
"github.com/craigpastro/pgmq-go"
)
func main() {
ctx := context.Background()
q, err := pgmq.New(ctx, "postgres://postgres:password@localhost:5432/postgres")
if err != nil {
panic(err)
}
err = q.CreateQueue(ctx, "my_queue")
if err != nil {
panic(err)
}
id, err := q.Send(ctx, "my_queue", json.RawMessage(`{"foo": "bar"}`))
if err != nil {
panic(err)
}
msg, err := q.Read(ctx, "my_queue", 30)
if err != nil {
panic(err)
}
// Archive the message by moving it to the "pgmq.a_<queue_name>" table.
// Alternatively, you can `Delete` the message, or read and delete in one
// call by using `Pop`.
_, err = q.Archive(ctx, "my_queue", id)
if err != nil {
panic(err)
}
}
We ❤️ contributions.