Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
いまさらdatabase/sql / 2018-12-18
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
rock619
December 18, 2018
Technology
1
1.2k
いまさらdatabase/sql / 2018-12-18
golang.tokyo #20
rock619
December 18, 2018
Tweet
Share
Other Decks in Technology
See All in Technology
Lambda Web AdapterでLambdaをWEBフレームワーク利用する
sahou909
0
170
実践 Datadog MCP Server
nulabinc
PRO
2
240
VPCエンドポイント意外とお金かかるなぁ。せや、共有したろ!
tommy0124
1
690
AI実装による「レビューボトルネック」を解消する仕様駆動開発(SDD)/ ai-sdd-review-bottleneck
rakus_dev
0
160
Claude Code 2026年 最新アップデート
oikon48
14
11k
OSC仙台プレ勉強会 AlmaLinuxとは
koedoyoshida
0
190
20260311 ビジネスSWG活動報告(デジタルアイデンティティ人材育成推進WG Ph2 活動報告会)
oidfj
0
350
OCI技術資料 : コンピュート・サービス 概要
ocise
4
54k
楽しく学ぼう!ネットワーク入門
shotashiratori
1
470
非情報系研究者へ送る Transformer入門
rishiyama
13
8.6k
Zeal of the Convert: Taming Shai-Hulud with AI
ramimac
0
150
生成AI活用でQAエンジニアにどのような仕事が生まれるか/Support Required of QA Engineers for Generative AI
goyoki
1
260
Featured
See All Featured
New Earth Scene 8
popppiees
1
1.7k
Designing Powerful Visuals for Engaging Learning
tmiket
0
280
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
53k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
GitHub's CSS Performance
jonrohan
1032
470k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
150
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
From π to Pie charts
rasagy
0
150
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
180
The untapped power of vector embeddings
frankvandijk
2
1.6k
Transcript
いまさら database/sql いまさら database/sql 2018-12-18 rock619
DB 接続 DB 接続 import ( "database/sql" _ "github.com/go-sql-driver/mysql" )
func main() { db, err := sql.Open("mysql", "user:password@protocol(host: if err != nil { panic(err) } defer db.Close() }
database driver の import database driver の import database/sql/driver パッケージ
database driver のinterface が定義されている 使用するdriver のimport driver のinterface の実装 github.com/go-sql-driver/mysql/
sql.DB sql.DB コネクションのプールを管理してくれる 複数のゴルーチンから使っても安全
sql.Open() sql.Open() 1 回だけでいい いちいちClose() する必要はない コネクションが作られるわけではない 接続できるかはPing() で確かめる必要あり
Ping() Ping() err = db.Ping() if err != nil {
// do something here }
database/sql/driver database/sql/driver Queryer...? type Queryer interface { Query(query string, args
[]Value) (Rows, error) } https://www.google.com/search?q=queryer
QueryRow() QueryRow() var name string err = db.QueryRow("select name from
users where id = ?", 1).Sc if err != nil { return err } fmt.Println(name)
QueryRow() QueryRow() var name string err = db.QueryRow("select name from
users where id = ?", 1).Sc switch err { case sql.ErrNoRows: return "" case nil: return name default: return err }
Null Null type NullString struct { String string Valid bool
// Valid is true if String is not NULL }
Scanner Scanner // Scan implements the Scanner interface. func (ns
*NullString) Scan(value interface{}) error { if value == nil { ns.String, ns.Valid = "", false return nil } ns.Valid = true return convertAssign(&ns.String, value) }
Valuer Valuer // Value implements the driver Valuer interface. func
(ns NullString) Value() (driver.Value, error) { if !ns.Valid { return nil, nil } return ns.String, nil }
sql.DBStats sql.DBStats MaxOpenConnections SetMaxOpenConns() で設定できる デフォルト: 0 (0 以下だと無制限)
sql.DBStats sql.DBStats 1.11 で追加されたもの OpenConnections コネクション数 InUse 使用中のコネクション数 Idle OpenConnections
- InUse
sql.DBStats sql.DBStats WaitCount コネクション待ちが発生した回数 WaitDuration 待った総時間 MaxIdleClosed MaxIdleConns を超えたためclose されたコネク
ション数 MaxLifetimeClosed MaxLifetime を超えたためclose されたコネクシ ョン数
参考 参考 https://golang.org/pkg/database/sql/ https://golang.org/pkg/database/sql/driver/ https://github.com/golang/go/wiki/SQLInterface