Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/getsentry/sentry-go v0.10.0
github.com/go-errors/errors v1.1.1 // indirect
github.com/google/go-cmp v0.5.4
github.com/mattn/go-sqlite3 v1.14.7
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/mediocregopher/radix/v3 v3.4.2/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8=
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
Expand Down
7 changes: 7 additions & 0 deletions internal/examples/basic/databases/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# サンプルリスト

このディレクトリには以下のサンプルがあります。

|file|example name|note|
|----|------------|----|
|open.go|db\_open|sql.Open 関数の使い方についてサンプルです|
6 changes: 6 additions & 0 deletions internal/examples/basic/databases/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package databases

const (
Driver string = "sqlite3" // ドライバ名
Dsn string = "chinook.db" // 接続文字列
)
4 changes: 4 additions & 0 deletions internal/examples/basic/databases/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package databases -- database/sql パッケージについてのサンプルが配置されているパッケージです。
*/
package databases
19 changes: 19 additions & 0 deletions internal/examples/basic/databases/examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package databases

import (
"github.com/devlights/try-golang/pkg/mappings"
)

type (
register struct{}
)

// NewRegister -- このパッケージ用のサンプルを登録する mappings.Register を生成します。
func NewRegister() mappings.Register {
return new(register)
}

// Regist -- 登録します.
func (r *register) Regist(m mappings.ExampleMapping) {
m["db_open"] = Open
}
50 changes: 50 additions & 0 deletions internal/examples/basic/databases/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package databases

import (
"database/sql"

"github.com/devlights/gomy/output"
// Goの標準ライブラリにはDBのドライバは含まれていないため
// 対象となるDBのドライバをインポートしておく必要がある
_ "github.com/mattn/go-sqlite3"
)

// Open は、sql.Open 関数の使い方についてサンプルです.
//
// REFERENCES:
// - https://pkg.go.dev/database/sql
// - https://github.com/golang/go/wiki/SQLDrivers
// - https://github.com/golang/go/wiki/SQLInterface
// - http://go-database-sql.org/
// - https://sourjp.github.io/posts/go-db/
func Open() error {
var (
db *sql.DB // データベース
err error // エラー
)

//
// ドライバとDSNを指定してデータベースとの接続
//
// sql.Open では、実際に接続されていない場合がある。
// (遅延評価となっており、最初に操作を行った際に実際に接続される)
// なので、クエリを発行する前に接続がちゃんと出来ているかどうかを
// 確認したい場合は、 (*sql.DB).Ping or PingContext を利用して
// 確認することも出来る。
//
// ファイルなどと同様に使い終わったらCloseする
//
if db, err = sql.Open(Driver, Dsn); err != nil {
return err
}

defer func() {
if err = db.Close(); err != nil {
output.Stderrf("[Error]", "db.Close: %s", err)
}
}()

output.Stdoutf("[sql.Open]", "%#v\n", db)

return nil
}
2 changes: 2 additions & 0 deletions internal/examples/basic/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/devlights/try-golang/internal/examples/basic/constants"
"github.com/devlights/try-golang/internal/examples/basic/convert"
"github.com/devlights/try-golang/internal/examples/basic/cryptos"
"github.com/devlights/try-golang/internal/examples/basic/databases"
"github.com/devlights/try-golang/internal/examples/basic/defers"
"github.com/devlights/try-golang/internal/examples/basic/embeds"
"github.com/devlights/try-golang/internal/examples/basic/enum"
Expand Down Expand Up @@ -72,6 +73,7 @@ func (r *register) Regist(m mappings.ExampleMapping) {
constants.NewRegister().Regist(m)
convert.NewRegister().Regist(m)
cryptos.NewRegister().Regist(m)
databases.NewRegister().Regist(m)
defers.NewRegister().Regist(m)
embeds.NewRegister().Regist(m)
enum.NewRegister().Regist(m)
Expand Down