Skip to content

Instantly share code, notes, and snippets.

@artyom
Created August 7, 2014 09:03
Show Gist options
  • Select an option

  • Save artyom/76410b50be55d2d1b60c to your computer and use it in GitHub Desktop.

Select an option

Save artyom/76410b50be55d2d1b60c to your computer and use it in GitHub Desktop.
Unmarshal Postgresql's json type in Go
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "user=artyom dbname=artyom sslmode=disable")
if err != nil {
log.Fatal(err)
}
var buf []byte
if err := db.QueryRow(query).Scan(&buf); err != nil {
log.Fatal(err)
}
var items []int
if err := json.Unmarshal(buf, &items); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", items)
}
const query = `select '[1,2,3]'::json as foo`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment