Created
August 7, 2014 09:03
-
-
Save artyom/76410b50be55d2d1b60c to your computer and use it in GitHub Desktop.
Unmarshal Postgresql's json type in Go
This file contains hidden or 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 ( | |
| "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