gpt4 book ai didi

postgresql - 使用 github.com/mgutz/dat 库运行示例脚本

转载 作者:行者123 更新时间:2023-12-01 22:24:12 26 4
gpt4 key购买 nike

由于历史原因,我尝试使用 github.com/mgutz/dat 运行一个简化的示例脚本。 ,一个 Postgres 数据访问工具包。我尝试在这个 repo 中复制示例脚本 https://github.com/kurtpeek/postgres-update , 如下 main.go :

package main

import (
"database/sql"
"fmt"
"time"

_ "github.com/lib/pq"
"gopkg.in/mgutz/dat.v1"
runner "gopkg.in/mgutz/dat.v1/sqlx-runner"
)

// global database (pooling provided by SQL driver)
var DB *runner.DB

func init() {
// create a normal database connection through database/sql
db, err := sql.Open("postgres", "dbname=dat_test user=dat password=!test host=localhost sslmode=disable")
if err != nil {
panic(err)
}

// ensures the database can be pinged with an exponential backoff (15 min)
runner.MustPing(db)

// set to reasonable values for production
db.SetMaxIdleConns(4)
db.SetMaxOpenConns(16)

// set this to enable interpolation
dat.EnableInterpolation = true

// set to check things like sessions closing.
// Should be disabled in production/release builds.
dat.Strict = false

// Log any query over 10ms as warnings. (optional)
runner.LogQueriesThreshold = 10 * time.Millisecond

DB = runner.NewDB(db, "postgres")
}

type Post struct {
ID int64 `db:"id"`
Title string `db:"title"`
Body string `db:"body"`
UserID int64 `db:"user_id"`
State string `db:"state"`
UpdatedAt dat.NullTime `db:"updated_at"`
CreatedAt dat.NullTime `db:"created_at"`
}

func main() {
var post Post
err := DB.
Select("id, title").
From("posts").
Where("id = $1", 13).
QueryStruct(&post)
fmt.Println("Title", post.Title)
}

但是,该脚本无法编译,并且出现以下错误:
> go run main.go
go: finding github.com/mgutz/logxi latest
build command-line-arguments: cannot load github.com/mgutz/logxi: cannot find module providing package github.com/mgutz/logxi

这可能是因为该 repo ( https://github.com/mgutz/logxi ) 中的包位于 v1 中。目录。

有什么办法可以解决这个问题,而不是 fork mgutz/dat repo 并修复其依赖关系?

最佳答案

原来有gopkg.in/mgutz/dat.v2 (不是 v1 )确实有效。这是修改后的脚本:

package main

import (
"database/sql"
"fmt"
"time"

_ "github.com/lib/pq"

"gopkg.in/mgutz/dat.v2/dat"
runner "gopkg.in/mgutz/dat.v2/sqlx-runner"
)

// global database (pooling provided by SQL driver)
var DB *runner.DB

func init() {
// create a normal database connection through database/sql
db, err := sql.Open("postgres", "dbname=postgres user=postgres password=mypassword host=localhost sslmode=disable")
if err != nil {
panic(err)
}

// ensures the database can be pinged with an exponential backoff (15 min)
runner.MustPing(db)

// set to reasonable values for production
db.SetMaxIdleConns(4)
db.SetMaxOpenConns(16)

// set this to enable interpolation
dat.EnableInterpolation = true

// set to check things like sessions closing.
// Should be disabled in production/release builds.
dat.Strict = false

// Log any query over 10ms as warnings. (optional)
runner.LogQueriesThreshold = 10 * time.Millisecond

DB = runner.NewDB(db, "postgres")
}

type Post struct {
ID int64 `db:"id"`
Title string `db:"title"`
Body string `db:"body"`
UserID int64 `db:"user_id"`
State string `db:"state"`
UpdatedAt dat.NullTime `db:"updated_at"`
CreatedAt dat.NullTime `db:"created_at"`
}

func main() {
var post Post
if err := DB.
Select("id, title").
From("posts").
Where("id = $1", 13).
QueryStruct(&post); err != nil {
panic(err)
}
fmt.Println("Title", post.Title)
}

为了使脚本运行,我运行了一个 Docker 容器,如下所示:
docker run --name mypostgres -p "5432:5432" -e POSTGRES_PASSWORD=mypassword -d postgres

使用连接到它
psql --host=localhost --port=5432 --username=postgres --dbname=postgres

并输入预期的表格和行:
postgres=# create table posts (id int, title varchar(50), body varchar(50), user_id int, state varchar(50), updated_at time, created_at time);
CREATE TABLE

postgres=# insert into posts (id, title, body, user_id, state, updated_at, created_at) values (13, 'My first post', 'Hello, world!', 1, 'CA', now(), now());
INSERT 0 1

打印预期的输出:
> go run main.go
Title My first post

关于postgresql - 使用 github.com/mgutz/dat 库运行示例脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61053175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com