“ pgx —— PostgreSQL 的纯 Go 驱动程序和工具包 ”
pgx 是用于 PostgreSQL 的纯 Go 驱动程序和工具包。 pgx 驱动程序是一个低级、高性能的接口,它 expose 了 PostgreSQL 特有的功能,如 LISTEN / NOTIFY 和 COPY。它还包括一个标准database/sql接口适配器。 工具包组件是一组相关的软件包,用于实现 PostgreSQL 的功能,如解析线协议和 PostgreSQL 与 Go 之间的类型映射。这些底层软件包可用于实现替代驱动程序、代理、负载平衡器、逻辑复制客户端等。 用法示例
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5"
)
func main() {
// urlExample := "postgres://username:password@localhost:5432/database_name"
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var name string
var weight int64
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(name, weight)
}
特性 - 支持大约 70 种不同的 PostgreSQL 类型 - 自动语句准备和缓存 - 批量查询 - 单次往返查询方式 - 完整的 TLS 连接控制 - 对自定义类型的二进制格式支持允许更快的编码/解码 - COPY协议支持,实现更快的批量数据加载 - Tracing 和 logging 支持 - 带有 after-connect hook 的连接池,用于任意连接设置 - LISTEN/NOTIFY - 将 PostgreSQL 数组转换为整数、浮点数和字符串的 Go 切片映射 - hstore支持 - json和jsonb支持 - 将inet和cidrPostgreSQL 类型映射为netip.Addr和netip.Prefix - Large object 支持 - NULL 映射为pointer to pointer - 支持自定义类型的database/sql.Scanner和database/sql/driver.Valuer接口 - 通知响应处理 - 使用保存点模拟嵌套事务
每日一言
"<!doctype html> <html lang="Zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="robots" content="noindex,nofollow"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <meta name="renderer" content="webkit"> <title>您请求的文件不存在!</title> <link type="text/css" rel="stylesheet" href="https://www.oick.cn/css/404.css" /> <link rel="shortcut icon" href="http://www.oick.cn/favicon.ico" type="image/x-icon" /> </head> <body> <div id="wrap"> <div> <img src="https://www.oick.cn/imgs/404.png" alt="404" /> </div> <div id="text"> <strong> <span></span> <a href="javascript:history.back()">返回上一页</a> </strong> </div> </div> <div class="animate below"></div> <div class="animate above"></div> </body> </html>"