Skip to content

Instantly share code, notes, and snippets.

@abraithwaite
Created July 10, 2017 16:19
Show Gist options
  • Save abraithwaite/f9cd2dd6a9311c4bd1633e5525843a8a to your computer and use it in GitHub Desktop.
Save abraithwaite/f9cd2dd6a9311c4bd1633e5525843a8a to your computer and use it in GitHub Desktop.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
const uri = "postgres://user:pass@localhost:5434/dev?sslmode=disable"
const query = `
select cast (:quote as text)
`
type Embedded struct {
Quote string
}
func main() {
var x = struct {
Embedded
Quote string `db:"quote"`
}{
Embedded: Embedded{Quote: "original"},
Quote: "override",
}
v, _ := json.Marshal(x)
log.Println(string(v))
dbx, err := sqlx.Open("postgres", uri)
if err != nil {
panic(err)
}
stmt, err := dbx.PrepareNamed(query)
if err != nil {
panic(err)
}
var res string
err = stmt.GetContext(context.Background(), &res, x)
if err != nil {
log.Println(err)
}
fmt.Println(res)
}
09:18:02 $ go run test.go
2017/07/10 09:18:03 {"Quote":"override"}
original
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment