Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
golang-bob.pdf
Search
Yura Iwatsuka
October 09, 2026
Programming
72
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
golang-bob.pdf
Yura Iwatsuka
October 09, 2026
More Decks by Yura Iwatsuka
See All by Yura Iwatsuka
Simple Storage Service(S3) is not simple
iwatsukayura
0
210
Other Decks in Programming
See All in Programming
最近”作らせてる” ローカルAIアプリの話
akit37
0
150
Jetpack Compose Mechanisms
skydoves
2
510
Kubernetes祭り #2 OP/ED
bells17
0
100
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
340
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.1k
ジョジョの世界をXRで再現する
mofurune
0
130
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
320
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
450
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
550
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
200
カツオ、ご期待ください
suneo3476
0
150
残高管理から台帳サービスへの進化
artoy
0
170
Featured
See All Featured
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
570
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
340
Crafting Experiences
bethany
1
370
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Speed Design
sergeychernyshev
33
2.1k
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
720
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
320
Unsuck your backbone
ammeep
672
58k
Transcript
これから来るかもしれないBob というライブラリを紹介します kamakura.go #10 株式会社スリーシェイク Sreake事業部 IwatsukaYura Copyright © 3-shake,
Inc. All Rights Reserved.
自己紹介 岩塚由楽(IwatsukaYura / @yura_builder) 所属:スリーシェイク 経歴:ソフトウェアエンジニア2年 Go言語大好き 興味:オブザーバビリティ、AWS ニックネーム:Chris Copyright
© 3-shake, Inc. All Rights Reserved. 2
アジェンダ 01 DBアクセスの選択肢と困りごと 02 Bobの紹介と使い方 03 GORMとの比較 04 sqlcとの比較 05
Bobの制約 06 まとめ 3
01 DBアクセスの選択肢と困りごと Copyright © 3-shake, Inc. All Rights Reserved. 4
DBアクセスは、何を起点に書くかで選ぶ 主な起点 使うものの例 自分で書くもの SQLを直接書く database/sql・pgx・sqlx SQLと、引数・結果の扱い SQLから生成する sqlc・Bob SQLとスキーマ
DBから生成する SQLBoiler・Bob DBスキーマと、型付きの条件 Goで定義する GORM・Bun・ent モデル/スキーマの定義 https://go.dev/doc/database/ https://bob.stephenafamo.com/docs/code-generation/intro/ 5
GORM:falseにしたいのに、更新されない 例:ユーザーを無効にする db.Model(&user).Updates( User{Active: false}, ) 意図とのずれ structのUpdatesは、既定でゼロ値 を更新しない 「未指定」と「falseへの変更」をど
う分ける? 期待:active = false 実際:activeが更新対象から外れる Select / mapで回避できるが、書き 方を意識する https://gorm.io/docs/update.html 6
sqlc:検索条件が増えると、SQLも複雑になる 例:ユーザー検索の画面 role:すべて / admin / member active:すべて / 有効
/ 無効 次は名前や登録日でも絞りたい 入力がない項目では、絞り込まない。 任意条件をどう書く? WHERE ($1::text IS NULL OR role = $1) 1本のSQLなら、条件を無効にする分岐 が増える。 SQLを分けるなら、条件の組み合わせを 管理する。 https://docs.sqlc.dev/en/latest/howto/named_parameters.html 7
02 Bobの紹介と使い方 Copyright © 3-shake, Inc. All Rights Reserved. 8
SQLBoilerのREADMEで、Bobが紹介されている SQLBoilerはメンテナンスモード。代替候補としてBobを紹介。 Bobは、SQLBoilerのメンテナーが開発した別プロジェクト。 https://github.com/aarondl/sqlboiler#alternatives https://bob.stephenafamo.com/vs/sqlboiler/ 9
Bobは、型を生成してから条件を組み立てられる DBスキーマ 手書きSQL → → モデルを生成 User / UserSetter 関数と結果の型を
生成 → → 生成した型で 検索・更新 With()で 検索条件を追加 どちらから始めても、Goで条件を追加できる。 SQLの組み立てを支えるのがQuery Builder。単体でも使える。 https://bob.stephenafamo.com/docs/code-generation/intro/ https://bob.stephenafamo.com/docs/code-generation/queries/ 10
共通のusersテーブルから、Goの型を生成する PostgreSQLへ適用 ↓ bobgen-psqlで生成 ↓ モデル・Setter・条件の型 マイグレーションは別途管理。 https://bob.stephenafamo.com/docs/code-generation/intro/ https://bob.stephenafamo.com/docs/code-generation/psql/ 11
良いところは、型付きの条件を組み合わせられること 生成された列・条件をIDEで補完できる 入力がある条件だけ、Goのifでクエリに追加できる 同じ条件を、複数の検索で部品として再利用できる SQLを先に書きたい箇所は、SQLファイルからも生成できる https://bob.stephenafamo.com/docs/code-generation/usage/ https://bob.stephenafamo.com/docs/code-generation/queries/ 12
03 GORMとの比較 Copyright © 3-shake, Inc. All Rights Reserved. 13
GORMとの比較①:文字列で書く列名 GORMの文字列条件では、列名 の誤記はGoの型検査を通る。 Bobの生成フィルターなら、存在 しないフィールドや値の型違い を検出できる。 https://gorm.io/docs/the_generics_way.html https://bob.stephenafamo.com/docs/code-generation/usage/ 14
GORMとの比較②:falseへの更新 structを渡すUpdates()は、既定 でゼロ値を除外する。 falseを書き込みたいときは、 Selectやmapで明示できる。 https://gorm.io/docs/update.html 15
Userは読む型、UserSetterは書く型 型 表せる状態 使う場所 null.Val[T] NULL / 値 UserのNULL許容列 omit.Val[T]
未指定 / 値 UserSetterのNOT NULL列 omitnull.Val[T] 未指定 / NULL / 値 UserSetterのNULL許容列 https://github.com/aarondl/opt https://bob.stephenafamo.com/docs/code-generation/usage/ 16
INSERT:未指定はDEFAULT、falseは値として渡す Activeはfalseを明示。 Roleは未指定なので、DBの DEFAULTでmemberになる。 One()で挿入した行を受け取る。 https://bob.stephenafamo.com/docs/code-generation/usage/ https://github.com/aarondl/opt 17
UPDATE:false・NULL・未指定を分けて書く Active → false Nickname → NULL Name・Role → 変更しない
INSERTと同じSetter。 https://bob.stephenafamo.com/docs/code-generation/usage/ https://github.com/aarondl/opt 18
GORMにも型付きAPIや回避策はある GORMでできること Bobを選ぶ理由 Genで型付きの列・条件 DBスキーマから型を生成したい Generics APIでも文字列条件は使う Setterで更新の意図を表したい Select /
mapでゼロ値を更新 SQLの句を細かく組み立てたい https://gorm.io/gen/query.html https://gorm.io/docs/the_generics_way.html 19
04 sqlcとの比較 Copyright © 3-shake, Inc. All Rights Reserved. 20
sqlcで悩みやすいのは、条件が増える検索 例:ユーザー検索画面 sqlcの出発点 roleを指定したときだけ絞る SQLファイルを先に書く activeは未指定 / true / false
クエリごとにGoの関数ができる 並び順やページサイズも変える 条件の組み合わせをSQL側で表す https://docs.sqlc.dev/en/latest/tutorials/getting-started-postgresql.html https://docs.sqlc.dev/en/latest/howto/named_parameters.html 21
sqlcでも、任意条件はSQLで表現できる PostgreSQL用に型を明示。 NULLなら条件を無効にする。 任意条件が増えると、SQL内の 分岐も増える。 この形で十分ならsqlcも有力。 https://docs.sqlc.dev/en/latest/howto/named_parameters.html 22
Bobなら、入力がある条件だけ追加する roleはstring、activeは*bool。 nilならactive条件を付けず、 falseならfalseで絞る。 条件の組み合わせをGoで表せ る。 https://bob.stephenafamo.com/docs/code-generation/usage/ 23
手書きSQLから生成した後も、条件を足せる 固定部分はSQLファイルに書 く。 生成されたクエリへWith()で条 件を追加する。 SQL起点でも、Goで組み立てら れる。 https://bob.stephenafamo.com/docs/code-generation/queries/ 24
05 Bobの制約 Copyright © 3-shake, Inc. All Rights Reserved. 25
自動タイムスタンプと論理削除は標準機能にない ほしい動作 Bobで用意するもの created_at / updated_atの自動更新 DBのDEFAULT・トリガー、または明示的な更新 Deleteを論理削除へ切り替える deleted_atのUPDATEを自分で実装 通常検索から削除済みを除外する
共通条件・Viewなどで除外規則を実装 https://bob.stephenafamo.com/docs/code-generation/ignored-features/ https://gorm.io/docs/delete.html 26
導入時は、再生成とSQLの確認を手順にする スキーマ変更 → 再生成 → 差分・コンパイル・実行を確認 型で検査できるのは、生成した型を使う範囲 DB方言・With()の規則・バージョン差は、出力SQLで確認 https://bob.stephenafamo.com/docs/code-generation/intro/ https://bob.stephenafamo.com/docs/code-generation/queries/
27
06 まとめ Copyright © 3-shake, Inc. All Rights Reserved. 28
Bobの特徴は、生成した型と動的なクエリ DBスキーマから型を生成し、列と値の型を使って書ける Setterで、未指定・値・NULLを区別して書き込める Goの条件分岐でクエリを組み立て、手書きSQLにも条件を足せる 試すなら、任意条件の多い検索APIを1本から https://bob.stephenafamo.com/docs/code-generation/usage/ https://bob.stephenafamo.com/docs/code-generation/queries/ 29
https://iwatsukayura.github.io/2026/10/golang-bob/
補足:生成されたUserとUserSetter 読み取りと書き込みで、表した い状態が違う。 2列だけ抜粋。タグや他の列は省 略。 https://bob.stephenafamo.com/docs/code-generation/usage/ 31
補足:条件を並べるとANDになる 有効なユーザー、かつNickname がNULL。 条件は生成された型で書く。 https://bob.stephenafamo.com/docs/code-generation/usage/ 32
補足:ORはWhereOrでまとめる 役割がadmin、またはNickname に値がある。 psqlはPostgreSQL用パッケー ジ。 https://bob.stephenafamo.com/docs/code-generation/usage/ 33
補足:複数INSERTでも、列の未指定を表せる Activeはどちらも未指定。 DBのDEFAULT TRUEを使う。 goroのRoleはmember、rokuro はadmin。 https://bob.stephenafamo.com/docs/code-generation/usage/ 34
補足:Deleteは行を削除する 上:取得済みモデルの主キーで 削除。 下:条件に一致する行を削除 し、件数を受け取る。 論理削除への自動変換はない。 https://bob.stephenafamo.com/docs/code-generation/usage/ https://bob.stephenafamo.com/docs/code-generation/ignored-features/ 35
補足:RunInTxは戻り値でCommit/Rollbackする fnがnilを返す → Commitを試みる fnがエラーを返す → Rollbackを試みる RunInTxの戻り値も確認する。 https://github.com/stephenafamo/bob/blob/v0.50.0/stdlib.go 36
補足:bob.Executorなら、DBもTxも渡せる deactivateGuestsの処理部分。 引数はctxとbob.Executor。 DB/Txの選択は呼び出し側で決 める。 https://github.com/stephenafamo/bob/blob/v0.50.0/exec.go https://github.com/stephenafamo/bob/blob/v0.50.0/stdlib.go 37
補足:RunInTxが自動で扱わないこと fn内のpanicをrecoverしない。自動Rollbackの保証はない 通常のエラーで失敗を返す設計にする bob.TxにはRunInTxがない。SAVEPOINT管理は別途設計 「入れ子の機能がない」と「DBで実現できない」は別 https://github.com/stephenafamo/bob/blob/v0.50.0/stdlib.go https://github.com/stephenafamo/bob/blob/v0.50.0/exec.go 38
補足:Query Builderだけでも使える SELECT / FROM / WHEREをGo で組み立てる。 値はArg()で渡す。 この書き方だけでは、列の存在
や値の型までは検査しない。 https://bob.stephenafamo.com/docs/query-builder/principles/ https://bob.stephenafamo.com/docs/query-builder/ 39
補足:SQLと引数を確認できる role="admin"、active=trueの 例。SELECT列は表示を省略。 bob.Build(ctx, q)で確認し、必要 ならDBでEXPLAINする。 https://pkg.go.dev/github.com/stephenafamo/
[email protected]
#Build 40
補足:With()で句がどう変わるか WHEREは既存条件へANDで追加される ORDER BYは追加。既存の並び順が置き換わるわけではない SQLite / PostgreSQLの単純SELECTでは、LIMIT / OFFSETを置換 返り値の型は生成時に固定。SELECT列の構造変更は再生成する
https://bob.stephenafamo.com/docs/code-generation/queries/ https://github.com/stephenafamo/bob/blob/v0.50.0/orm/query.go 41
補足:動くサンプルと確認範囲 Bob v0.50.0 / PostgreSQL / Go 1.26.4 SELECT・AND/OR・動的条件・With()・INSERT・UPDATE・DELETE RunInTx:コミット/ロールバックと、Executorの共通化
demo-postgres/README.mdに実行・再生成の手順 公式仕様の再確認:2026年10月8日 https://github.com/stephenafamo/bob/releases/tag/v0.50.0 https://bob.stephenafamo.com/docs/code-generation/psql/ 42
補足:公式資料 SQLBoiler:README / Bobとの比較 Bob:Code Generation / Usage / Queries
/ opt GORM:Update / Gen Query / Generics API sqlc:PostgreSQL tutorial / Naming parameters トランザクション:Bob v0.50.0のstdlib.go 43