2018-04-03 09:25
浏览 270


I am trying to retrieve a set of rows from postgresql and trying to assign it in an array of structure. My code goes like this :

var test []Demo
    err := sqlx.Get(db,&test, `
                    select *
                    from demo
                    where id = $1`,
            if err != nil {
                    fmt.Println("DatabaseExtraction Error")
                    return nil, errors.Wrap(err, "select error")
            } else {
                    fmt.Println("No Extraction Error")

I have a structure like this :

type Demo struct {
        ID         int64         `db:"id"`
        Name       string        `db:"name"`

But I am getting an error like this Error scannable dest type slice with >1 columns (2) in result (code: 2) Also I don't get any error if I replace var test []Demo with var test Demo.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doucai1901 2018-04-03 09:29

    If you call Get it expects only one row to be returned. If you want to select multiple rows, use Select and pass a pointer to your slice. For example:

    var rows []Demo
    err := sqlx.Select(&rows, `SELECT * FROM "demo"`)
    // ...
    打赏 评论

相关推荐 更多相似问题