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.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
いまさらdatabase/sql / 2018-12-18
golang.tokyo #20
rock619
December 18, 2018
Other Decks in Technology
See All in Technology
「速く作る」から「正しく作る」へ ─ 生成AI時代の開発フロー改革の ロードマップと実行 ─
starfish719
0
9.3k
作って終わりにしない タイミーのセマンティックレイヤー育成の現在地
chanyou0311
3
1.7k
Djangoユーザが知っ得なPostgreSQL機能 - 設計の選択肢を増やす / Djang-use-PostgreSQL
soudai
PRO
0
210
MIERUNE JCT 発表資料「宇宙から伊能忠敬ごっこ」
syuchimu
0
200
探して_入れて_作って_使う_Agent_Skills___LT.pdf
peintangos
2
180
AI Adaptable なテストを整える工夫 / Ways to Make Your Tests AI-Adaptable
bitkey
PRO
3
230
Kubernetesにおける学習基盤とLLMOpsの概要
ry
1
120
Oracle Cloud Infrastructure IaaS 新機能アップデート 2026/3 - 2026/5
oracle4engineer
PRO
1
230
あなたの AI ワークスペースに、 専門コーダーを連れてくる - Amazon Quick Desktop 最新情報
kawaji_scratch
1
120
ポケモンの型をTypeScriptの型システムで表現してみた
subroh0508
0
360
ブロックチェーン / Blockchain
ks91
PRO
0
110
製造業のクラウド活用最適解〜AI,DXを加速するデータ基盤の作り方〜
hamadakoji
0
420
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
0
8.7k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
330
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
Docker and Python
trallard
47
3.9k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
390
So, you think you're a good person
axbom
PRO
2
2.1k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
200
The agentic SEO stack - context over prompts
schlessera
0
800
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
480
Visualization
eitanlees
152
17k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
380
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