File tree Expand file tree Collapse file tree 8 files changed +91
-0
lines changed
Expand file tree Collapse file tree 8 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ require (
1010 github.com/getsentry/sentry-go v0.10.0
1111 github.com/go-errors/errors v1.1.1 // indirect
1212 github.com/google/go-cmp v0.5.4
13+ github.com/mattn/go-sqlite3 v1.14.7
1314 github.com/pkg/errors v0.9.1 // indirect
1415 github.com/stretchr/testify v1.7.0 // indirect
1516 golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df
Original file line number Diff line number Diff line change @@ -87,6 +87,8 @@ github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
8787github.com/mattn/go-isatty v0.0.7 /go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s =
8888github.com/mattn/go-isatty v0.0.8 /go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s =
8989github.com/mattn/go-isatty v0.0.9 /go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ =
90+ github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA =
91+ github.com/mattn/go-sqlite3 v1.14.7 /go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU =
9092github.com/mattn/goveralls v0.0.2 /go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw =
9193github.com/mediocregopher/radix/v3 v3.4.2 /go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8 =
9294github.com/microcosm-cc/bluemonday v1.0.2 /go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc =
Original file line number Diff line number Diff line change 1+ # サンプルリスト
2+
3+ このディレクトリには以下のサンプルがあります。
4+
5+ | file| example name| note|
6+ | ----| ------------| ----|
7+ | open.go| db\_ open| sql.Open 関数の使い方についてサンプルです|
Original file line number Diff line number Diff line change 1+ package databases
2+
3+ const (
4+ Driver string = "sqlite3" // ドライバ名
5+ Dsn string = "chinook.db" // 接続文字列
6+ )
Original file line number Diff line number Diff line change 1+ /*
2+ Package databases -- database/sql パッケージについてのサンプルが配置されているパッケージです。
3+ */
4+ package databases
Original file line number Diff line number Diff line change 1+ package databases
2+
3+ import (
4+ "github.com/devlights/try-golang/pkg/mappings"
5+ )
6+
7+ type (
8+ register struct {}
9+ )
10+
11+ // NewRegister -- このパッケージ用のサンプルを登録する mappings.Register を生成します。
12+ func NewRegister () mappings.Register {
13+ return new (register )
14+ }
15+
16+ // Regist -- 登録します.
17+ func (r * register ) Regist (m mappings.ExampleMapping ) {
18+ m ["db_open" ] = Open
19+ }
Original file line number Diff line number Diff line change 1+ package databases
2+
3+ import (
4+ "database/sql"
5+
6+ "github.com/devlights/gomy/output"
7+ // Goの標準ライブラリにはDBのドライバは含まれていないため
8+ // 対象となるDBのドライバをインポートしておく必要がある
9+ _ "github.com/mattn/go-sqlite3"
10+ )
11+
12+ // Open は、sql.Open 関数の使い方についてサンプルです.
13+ //
14+ // REFERENCES:
15+ // - https://pkg.go.dev/database/sql
16+ // - https://github.com/golang/go/wiki/SQLDrivers
17+ // - https://github.com/golang/go/wiki/SQLInterface
18+ // - http://go-database-sql.org/
19+ // - https://sourjp.github.io/posts/go-db/
20+ func Open () error {
21+ var (
22+ db * sql.DB // データベース
23+ err error // エラー
24+ )
25+
26+ //
27+ // ドライバとDSNを指定してデータベースとの接続
28+ //
29+ // sql.Open では、実際に接続されていない場合がある。
30+ // (遅延評価となっており、最初に操作を行った際に実際に接続される)
31+ // なので、クエリを発行する前に接続がちゃんと出来ているかどうかを
32+ // 確認したい場合は、 (*sql.DB).Ping or PingContext を利用して
33+ // 確認することも出来る。
34+ //
35+ // ファイルなどと同様に使い終わったらCloseする
36+ //
37+ if db , err = sql .Open (Driver , Dsn ); err != nil {
38+ return err
39+ }
40+
41+ defer func () {
42+ if err = db .Close (); err != nil {
43+ output .Stderrf ("[Error]" , "db.Close: %s" , err )
44+ }
45+ }()
46+
47+ output .Stdoutf ("[sql.Open]" , "%#v\n " , db )
48+
49+ return nil
50+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import (
1111 "github.com/devlights/try-golang/internal/examples/basic/constants"
1212 "github.com/devlights/try-golang/internal/examples/basic/convert"
1313 "github.com/devlights/try-golang/internal/examples/basic/cryptos"
14+ "github.com/devlights/try-golang/internal/examples/basic/databases"
1415 "github.com/devlights/try-golang/internal/examples/basic/defers"
1516 "github.com/devlights/try-golang/internal/examples/basic/embeds"
1617 "github.com/devlights/try-golang/internal/examples/basic/enum"
@@ -72,6 +73,7 @@ func (r *register) Regist(m mappings.ExampleMapping) {
7273 constants .NewRegister ().Regist (m )
7374 convert .NewRegister ().Regist (m )
7475 cryptos .NewRegister ().Regist (m )
76+ databases .NewRegister ().Regist (m )
7577 defers .NewRegister ().Regist (m )
7678 embeds .NewRegister ().Regist (m )
7779 enum .NewRegister ().Regist (m )
You can’t perform that action at this time.
0 commit comments