Skip to content

Instantly share code, notes, and snippets.

@Telmo
Created June 15, 2016 11:55
Show Gist options
  • Save Telmo/1e8159b0056d44300b18a3d03e52acc6 to your computer and use it in GitHub Desktop.
Save Telmo/1e8159b0056d44300b18a3d03e52acc6 to your computer and use it in GitHub Desktop.
sqlx scannable dest type struct with >1 columns (16) in result
mysql> select * from builds where id='1';
*************************** 1. row ***************************
id: 1
name: test1
description: This is the redhat-6-4 test
custom_packages: http://test.net/builds/redhat/6/4/extra_packages
ssh_key: http://test.net/builds/id_rsa.pub
yum_repos: http://test.net/builds/redhat/6/extra_repos
postinstall: qq
os: redhat
major: 6
minor: 4
group: dev
shared: 1
created_by: dev
created_at: 2016-06-10 11:59:39
updated_at: 2016-06-10 11:59:50
partitioning_table: qq
1 row in set (0.00 sec)
# go run main.go
2016/06/15 11:46:44 scannable dest type struct with >1 columns (16) in result
exit status 1
package main
import (
"fmt"
"time"
"log"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Build struct {
id int
name string
description string
packages string `db:"custom_packages" json:"packages"`
sshkey string `db:"ssh_key" json:"sshkey"`
repos string `db:"yum_repos" json:"repos"`
postinstall string
ptable string `db:"partitioning_table" json:"ptable"`
os string
major string
minor string
group string
shared int
owner string `db:"created_by" json:"owner"`
createdat time.Time `db:"created_at" json:"createdat"`
updatedat time.Time `db:"updated_at" json:"updatedat"`
}
func main() {
dsn := "root:test@tcp(mysql:3306)/universalbuilds"
db, err := sqlx.Open("mysql", dsn)
if err != nil {
log.Fatal("kaboom")
}
defer db.Close()
stmt, err := db.Preparex(`SELECT * from builds where id=?`)
if err != nil {
log.Fatal("Prepare statement failed")
}
var b Build
err = stmt.Get(&b, 1)
if err != nil {
log.Fatal(err)
}
fmt.Println("%v", b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment