【问题标题】:sqlx - non-struct dest type struct with >1 columns (2)sqlx - 具有 >1 列的非结构 dest 类型结构 (2)
【发布时间】:2016-09-26 11:55:57
【问题描述】:

我已经搜索了错误,我发现了两个问题:

这是我的代码:

package main

import (
    "log"

    "github.com/jmoiron/sqlx"

    _ "github.com/lib/pq"
)

var schema = `
CREATE TABLE films (
  code int,
  name VARCHAR(10)
)`

type Film struct {
    code int
    name string
}

func main() {
    db, err := sqlx.Open("postgres", "user=demas password=root host=192.168.99.100 port=32768 dbname=mydb sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }

    db.MustExec(schema)

    tx := db.MustBegin()
    tx.MustExec("INSERT INTO films(code, name) VALUES($1, $2)", 10, "one")
    tx.MustExec("INSERT INTO films(code, name) VALUES($1, $2)", 20, "two")
    tx.Commit()

    films := []Film{}
    err = db.Select(&films, "SELECT * FROM public.films")
    if err != nil {
        log.Fatal(err)
    }

}

创建表并插入2条记录,但无法返回:

λ go run main.go
2016/09/26 14:46:04 non-struct dest type struct with >1 columns (2)
exit status 1

我该如何解决?

【问题讨论】:

    标签: go sqlx


    【解决方案1】:

    如果您导出字段,它会起作用:

    type Film struct {
        Code int
        Name string
    }
    

    注意大写(Code,而不是code)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      相关资源
      最近更新 更多