Sqlx - structure of unstructured type s> 1 column (2)

I was looking for an error and I have two questions:

  • This , but my question is not duplicated.
  • And this one , but there is no answer to 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 a table and inserts 2 records, but cannot return them:

 λ 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 this?

+6
source share
1 answer

It works if you export your fields:

 type Film struct { Code int Name string } 

Pay attention to upper case ( Code , not Code ).

+10
source

All Articles