Created
July 10, 2017 16:19
-
-
Save abraithwaite/f9cd2dd6a9311c4bd1633e5525843a8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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