diff options
author | EuAndreh <eu@euandre.org> | 2024-08-14 17:31:36 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-08-14 17:31:36 -0300 |
commit | 0b8d97f96dd4db0e50a1cb68a0740a55017c469b (patch) | |
tree | aeb75b31cec46c6de5a8e28aceabeb10143d98f2 /_example/json/json.go | |
parent | Remove most files from _example/ (diff) | |
download | golite-0b8d97f96dd4db0e50a1cb68a0740a55017c469b.tar.gz golite-0b8d97f96dd4db0e50a1cb68a0740a55017c469b.tar.xz |
git mv _example doc/examples/
Diffstat (limited to '_example/json/json.go')
-rw-r--r-- | _example/json/json.go | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/_example/json/json.go b/_example/json/json.go deleted file mode 100644 index 181934b..0000000 --- a/_example/json/json.go +++ /dev/null @@ -1,81 +0,0 @@ -package main - -import ( - "database/sql" - "database/sql/driver" - "encoding/json" - "fmt" - _ "github.com/mattn/go-sqlite3" - "log" - "os" -) - -type Tag struct { - Name string `json:"name"` - Country string `json:"country"` -} - -func (t *Tag) Scan(value interface{}) error { - return json.Unmarshal([]byte(value.(string)), t) -} - -func (t *Tag) Value() (driver.Value, error) { - b, err := json.Marshal(t) - return string(b), err -} - -func main() { - os.Remove("./foo.db") - - db, err := sql.Open("sqlite3", "./foo.db") - if err != nil { - log.Fatal(err) - } - defer db.Close() - - _, err = db.Exec(`create table foo (tag jsonb)`) - if err != nil { - log.Fatal(err) - } - - stmt, err := db.Prepare("insert into foo(tag) values(?)") - if err != nil { - log.Fatal(err) - } - defer stmt.Close() - _, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`) - if err != nil { - log.Fatal(err) - } - _, err = stmt.Exec(`{"name": "michael", "country": "usa"}`) - if err != nil { - log.Fatal(err) - } - - var country string - err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country) - if err != nil { - log.Fatal(err) - } - fmt.Println(country) - - var tag Tag - err = db.QueryRow("select tag from foo where tag->>'name' = 'mattn'").Scan(&tag) - if err != nil { - log.Fatal(err) - } - - fmt.Println(tag.Name) - - tag.Country = "日本" - _, err = db.Exec(`update foo set tag = ? where tag->>'name' == 'mattn'`, &tag) - if err != nil { - log.Fatal(err) - } - - err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country) - if err != nil { - log.Fatal(err) - } - fmt.Println(country) -} |