File: example_json_test.go

package info (click to toggle)
golang-github-jackc-pgx-v5 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,764 kB
  • sloc: sh: 88; sql: 21; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 620 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package pgtype_test

import (
	"context"
	"fmt"
	"os"

	"github.com/jackc/pgx/v5"
)

func Example_json() {
	conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
	if err != nil {
		fmt.Printf("Unable to establish connection: %v", err)
		return
	}

	type person struct {
		Name string `json:"name"`
		Age  int    `json:"age"`
	}

	input := person{
		Name: "John",
		Age:  42,
	}

	var output person

	err = conn.QueryRow(context.Background(), "select $1::json", input).Scan(&output)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(output.Name, output.Age)
	// Output:
	// John 42
}