douyimiao1993 2016-09-26 11:55
浏览 359
已采纳

sqlx-具有> 1列的非struct dest类型的struct(2)

I have searched the error and I have find two questions:

  • This one, but my question is not duplicate of it
  • And this one, but there is no answer in this question.

Here is my code:

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)
    }

}

It creates table and insert 2 records, but can not return them back:

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

How can I fix it ?

  • 写回答

1条回答 默认 最新

  • dongsuiying7773 2016-09-26 12:55
    关注

    It works if you export your fields:

    type Film struct {
        Code int
        Name string
    }
    

    Notice the uppercase (Code, not code).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程