【发布时间】:2020-04-06 13:23:29
【问题描述】:
由于历史原因,我正在尝试使用 Postgres 数据访问工具包 github.com/mgutz/dat 运行一个简化的示例脚本。我尝试在这个 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 目录中。
除了分叉 mgutz/dat 存储库并修复其依赖项之外,我有什么办法可以解决这个问题吗?
【问题讨论】:
标签: postgresql go