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
GoのGenericsによるslice操作との付き合い方
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
syumai
June 18, 2025
Programming
1.2k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GoのGenericsによるslice操作との付き合い方
golang.tokyo #39 の発表資料です
https://golangtokyo.connpass.com/event/353988/
syumai
June 18, 2025
More Decks by syumai
See All by syumai
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.8k
Oxlintのカスタムルールの現況
syumai
6
1.3k
Oxlintはいかにしてtsgolintのlint ruleを呼び出しているのか
syumai
2
1.4k
『[入門] Cloudflare Workers』本はなぜ誕生したのか
syumai
0
500
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
9
3.2k
知られているようで知られていない JavaScriptの仕様 4選
syumai
3
1.3k
CloudflareのSandbox SDKを試してみた
syumai
0
990
実践AIチャットボットUI実装入門
syumai
9
4.4k
ProxyによるWindow間RPC機構の構築
syumai
3
1.6k
Other Decks in Programming
See All in Programming
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
700
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
170
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
360
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
730
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
120
プロポーザルを書いてもらう
pvcresin
0
530
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
490
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
290
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
440
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
220
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.5k
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
Featured
See All Featured
Information Architects: The Missing Link in Design Systems
soysaucechin
0
1.1k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
370
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
200
Game over? The fight for quality and originality in the time of robots
wayneb77
1
240
What's in a price? How to price your products and services
michaelherold
247
13k
Utilizing Notion as your number one productivity tool
mfonobong
4
540
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
380
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
490
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Skip the Path - Find Your Career Trail
mkilby
1
180
Building AI with AI
inesmontani
PRO
1
1.1k
Transcript
Go のGenerics によるslice 操作との付き合い方 syumai golang.tokyo #39 (2025/6/18)
自己紹介 syumai ECMAScript 仕様輪読会 / Asakusa.go 主催 株式会社ベースマキナで管理画面のSaaS を開発中 Go
でGraphQL サーバー (gqlgen) や TypeScript でフロント エンドを書いています Software Design 2023 年12 月号から2025 年2 月号まで Cloudflare Workers の連載をしました Twitter ( 現𝕏): @__syumai Website: https://syum.ai
今日話すこと basemachina/lo の紹介 実装のモチベーション i. 従来のslice 操作の課題 ii. samber/lo 導入の課題
slice 変換機能の使い方 ( おまけ) イテレータの利用状況
basemachina/lo
basemachina/lo Generics を使って Map , Filter などの操作をslice に対して簡単に行えるライブラリ 型情報を使ったslice 以外の操作も必要に応じて追加している
numbers := []int{1, 2, 3, 4, 5} doubled := lo.Map(numbers, func(x int) int { return x * 2 }) fmt.Println(doubled) // [2, 4, 6, 8, 10] evens := lo.Filter(numbers, func(x int) bool { return x%2 == 0 }) fmt.Println(evens) // [2, 4] fmt.Printf("%T\n", lo.ToPtr(1)) // *int
現在使える機能 ( 全19 種類) * Array/Slice操作 - フィルタリング - Filter
- FilterWithIndex - 変換 - Map - MapWithError - MapWithIndex - MapWithIndexError - FlatMap - FlatMapWithIndex - 検索・判定 - Find - Every - Some - HasDuplicates - HasDuplicatesBy - 集約 - Reduce - ReduceWithIndex - 集合演算 - Intersect * Map操作 - Invert * ポインタ操作 - ToPtr - FromPtrOr
実装のモチベーション
1. 従来のslice 操作の課題
None
None
None
1. 従来のslice 操作の課題 range を使った詰め替えでは、うっかり事故る可能性がある lint を入れて防ぐしかない ( 私見) lo.Map
や lo.Filter などの関数を使ったコードの方が読むのが楽 行いたい操作が関数名から明確 slice 操作の実装の詳細を確認しなくていい
2. samber/lo 導入の課題
2. samber/lo 導入の課題 機能が充実しすぎている 全部を使いたい訳ではない 学習コストを問題視
https://pkg.go.dev/github.com/samber/lo
はてなさんの事例 https://github.com/hatena/godash 今はiter 推奨らしい
basemachina/lo のslice 変換機能の使い方
basemachina/lo のslice 変換機能の使い方 DB から取ってきたデータを変換してGraphQL resolver から返す処理 func (r *queryResolver)
Views(ctx context.Context) ([]*gql.View, error) { // []*model.View を取得 views, err := loader.ListViews(ctx, r.project.ID) if err != nil { return nil, err } // []*gql.View を返却 return lo.Map(views, gqlpresenter.View), nil }
basemachina/lo のslice 変換機能の使い方 basemachina/lo 無しの場合 func (r *queryResolver) Views(ctx context.Context)
([]*gql.View, error) { // []*model.View を取得 views, err := loader.ListViews(ctx, r.project.ID) if err != nil { return nil, err } // []*gql.View を返却 gqlViews := make([]*gql.View, len(views)) for i, v := range views { gqlViews[i] = gqlpresenter.View(v) } return gqlViews }
basemachina/lo のslice 変換機能の使い方 変換用のコードは使い回せるので別package で実装 lo.Map のシグニチャを変えて (item T, index
int) を (item T) だけにして いるのでこれでOK package gqlpresenter func View(v *model.View) *gql.View { return &gql.View{ ID: v.ID, Name: v.Name, Code: v.Code, } }
もし lo.Map のシグニチャが iteratee (item T, index int) だったら 変換処理側で合わせる場合
package gqlpresenter // 毎回 `_ int` を書かないといけない func View(v *model.View, _ int) *gql.View { return &gql.View{ ID: v.ID, Name: v.Name, Code: v.Code, } }
もし lo.Map のシグニチャが iteratee (item T, index int) だったら 変換処理を使う側で合わせる場合
func (r *queryResolver) Views(ctx context.Context) ([]*gql.View, error) { views, err := loader.ListViews(ctx, r.project.ID) if err != nil { return nil, err } // 毎回無名関数を書く return lo.Map(views, func (v *model.View, _ int) *gql.View { return gqlpresenter.View(v) }), nil }
basemachina/lo のslice 変換機能の使い方 変換時にエラーが発生しうる場合は lo.MapWithError を使う func (r *queryResolver) Actions(ctx
context.Context) ([]*gql.Action, error) { /* ... */ return lo.MapWithError(actions, gqlpresenter.Action) }
basemachina/lo のslice 変換機能の使い方 MapWithIndexError (MapWithError の内部で呼ぶ) の実装 https://github.com/samber/lo/pull/43 で提案 func
MapWithIndexError[T any, R any]( collection []T, iteratee func(item T, index int) (R, error)) ([]R, error) { result := make([]R, len(collection)) var err error for i, item := range collection { result[i], err = iteratee(item, i) if err != nil { return nil, err } } return result, nil }
( おまけ) イテレータの利用状況
イテレータ、積極的に使っていますか?
社内のイテレータの利用状況 社内では導入がほぼ進んでいない ほとんどがslice の変換だけで済んでしまうユースケースのみ
Go のxiter package proposal がClose データ列の変換目的でのイテレータ導入の理由は弱くなった https://github.com/golang/go/issues/61898
パフォーマンスチューニング目的での導入はありうる https://developers.cyberagent.co.jp/blog/archives/54653/
補足 - xiter 以外の選択肢 go-functional https://github.com/BooleanCat/go-functional Go のイテレータを使って関数型チックにコードを書けるライブラリ ヘルパー関数も充実している
まとめ
まとめ Generics を活用したslice 操作ライブラリの自前実装を持つのは便利 slice 操作のスタイルを統一できる イテレータを使わないslice 操作は残念ながらまだ現役
ご清聴ありがとうございました!