【问题标题】:Running an example script using the github.com/mgutz/dat library使用 github.com/mgutz/dat 库运行示例脚本
【发布时间】: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


    【解决方案1】:

    原来有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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 2016-01-02
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      相关资源
      最近更新 更多