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
【Go言語】イテレータ
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Tomoki Ota
September 11, 2024
Programming
130
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
【Go言語】イテレータ
Go1.23から追加されたイテレータについて解説しています。
今後増量予定。
Tomoki Ota
September 11, 2024
More Decks by Tomoki Ota
See All by Tomoki Ota
アーキテクチャの価値
tomo1227
0
130
アーキテクチャとビジネスロジック
tomo1227
0
140
【Go言語】テスト
tomo1227
0
110
【Go言語】クロージャ
tomo1227
0
360
【Go言語】エラーハンドリング
tomo1227
0
110
AIに淘汰されないようにするには?
tomo1227
0
110
【Go言語】range
tomo1227
0
110
【Go言語】break
tomo1227
0
120
【Go言語】反復処理
tomo1227
0
90
Other Decks in Programming
See All in Programming
golang-bob.pdf
iwatsukayura
0
130
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
150
ジョジョの世界をXRで再現する
mofurune
0
160
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
140
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
0
340
速習iPhone Duo対応
yuukiw00w
2
980
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
380
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
1k
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
150
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
Next.js: The final boss of Web Performance
guaca
0
190
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
31k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
The Cult of Friendly URLs
andyhume
79
7k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
280
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
500
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
The Limits of Empathy - UXLibs8
cassininazir
2
720
Transcript
Tomoki Ota ʲGoݴޠʳΠςϨʔλ
ΠςϨʔλͱʁ ֎෦ΠςϨʔλ&෦ΠςϨʔλ Iteratorύλʔϯ Visitorύλʔϯ δΣωϨʔλ GoͷΠςϨʔλ ΞδΣϯμ 2
ΠςϨʔλͱʁ
ΠςϨʔλͱʁ 4 ΠςϨʔλ(෮ࢠ) : σʔλू߹ͷཁૉʹॱ൪ʹΞΫηε͢ΔͨΊͷΈ
ΠςϨʔλͱʁ 5 ίϨΫγϣϯͷཁૉͷྻڍɾ෮ͷํ๏ʹɺ2ͭͷํ͕ࣜ͋Δɻ •෦ΠςϨʔλ •֎෦ΠςϨʔλ
֎෦ΠςϨʔλ&෦ΠςϨʔλ
֎෦ΠςϨʔλ 7 ίϯςφͷཁૉΛॱʹऔΓग़ͨ͢ΊͷผͷΦϒδΣΫτΛ༻ҙ͢Δํࣜ(Java, C++ͳͲ) ίϯςφͷཁૉΛॱʹऔΓग़ͨ͢ΊͷผͷΦϒδΣΫτΛΠςϨʔλ(Χʔιϧ)ͱݺͿɻ •࣮͕͍͠ •ίʔυ͕ෳࡶ •ෳͷίϯςφ͔ΒҰͭͣͭཁૉΛऔΓग़͠ ฒߦʹॲཧ͢ΔΑ͏ͳखଓ͖͕ಘҙ
෦ΠςϨʔλ 8 ݸʑͷཁૉ͝ͱͷॲཧΛදݱ͢ΔͷΛίϯςφɾΦϒδΣΫτͷϝιουʹ͠ɼ ϝιου͕ཁૉ͝ͱͷॲཧΛݺͼฦ͢λΠϓͷ܁Γฦ͠ํࣜ(RubyͳͲ) • ͏ͷ࡞Δͷ؆୯ • Ϋϩʔδϟ͕ͳ͍ͱϧʔϓຊମͱ֎ଆͱͰใΛڞ༗͢ΔͨΊʹ ͕ඞཁʹͳΔͷͰɺ͍উख͕ѱ͘ͳΔɻ
Itaratorύλʔϯ
Itaratorύλʔϯ 10 GoFʹΑͬͯఆٛ͞ΕͨσβΠϯύλʔϯͷ1ͭɻ ΠςϨʔλΛ༻͢Δ͜ͱͰɺϦετɺ ελοΫɺ πϦʔͳͲͷ ࣮ࡍͷσʔλߏΛҙࣝͤͣʹɺཁૉΛ୳ࡧ͢Δ͜ͱ͕Ͱ͖Δɻ
Itaratorύλʔϯ 11 ந ۩
ItaratorύλʔϯͷϝϦοτ 12 • ୯Ұͷݪଇɻ ͔͞Δ୳ࡧΞϧΰϦζϜΛผΫϥεʹநग़͢Δ͜ͱʹΑΓ ΫϥΠΞϯτɾίʔυͱίϨΫγϣϯͷཧ͕Մೳ • ΦʔϓϯɾΫϩʔζυͷݪଇɻ ৽͍͠छྨͷίϨΫγϣϯͱΠςϨʔλʔΛ࣮͠ طଘͷίʔυʹͳ͘͢͜ͱ͕Մೳ
• ֤ΠςϨʔλʔɾΦϒδΣΫτ͕ࣗͷ෮ঢ়ଶΛ͍࣋ͬͯΔͨΊ ಉ͡ίϨΫγϣϯΛฏߦͯ͠෮Մೳ • ಉ͡ཧ༝Ͱɺ ෮ΛҰ࣌தஅͯ͠ඞཁͳ࣌ʹ࠶։͢Δ͜ͱ͕Մೳ
ItaratorύλʔϯͷϝϦοτ 13 •ΞϓϦ͕୯७ͳίϨΫγϣϯͱͷΈಈ࡞͢Δ߹ ͜ͷύλʔϯͷద༻ͳՄೳੑ͕͋Δ •ಛఆͷಛघͳίϨΫγϣϯͷ߹ɺ ΠςϨʔλʔͷ༻ ཁૉΛݟͯߦ͘ΑΓඇޮͳՄೳੑ͕͋Δ
Visitorύλʔϯ
Itaratorύλʔϯ v.s Visitorύλʔϯ 15 Itaratorύλʔϯ : ίϨΫγϣϯͷཁૉʹॱʹ̍ͭͣͭΞΫηε͢Δ Visitorύλʔϯ : ίϨΫγϣϯͷཁૉΛ̍ͭ̍ͭ८ͬͯɺԿΒ͔ͷॲཧΛ
ߦ͍ͬͯ͘
Itaratorύλʔϯ v.s Visitorύλʔϯ 16
δΣωϨʔλ
δΣωϨʔλͱ 18 ΠςϨʔλΛ؆୯ʹ࡞Δ͜ͱ͕Ͱ͖ΔɻΠςϨʔλΛฦؔ͢ δΣωϨʔλؔͱ͍͏ɻ ·ͨɺδΣωϨʔλؔͰ࡞͞ΕͨΠςϨʔλͷ͜ͱΛ δΣωϨʔλͱݺͿ͜ͱ͋Δɻ
GoͷΠςϨʔλ
range over int
range over int 21 Go1.22Ҏ߱ Go1.22Ҏલ
range over func
range over func 23 GoͷΠςϨʔλͷσϑΝΫτελϯμʔυ
v1.23ҎલͷΠςϨʔλ 24 database/sql.Rows archive/tar.Reader.Next
range over func 25 ҎԼͷγάωνϟͷؔΛrangeͰ෮ॲཧͰ͖Δɻ
range over func 26
range over func 27 rangeͰyieldΛ༻͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳͬͨɻ
GoͷΠςϨʔλ 28 • Push(ඪ४)ΠςϨʔλ : 1ͭͣͭσʔλΛ͢ • PullΠςϨʔλ : 1ͭͣͭσʔλΛड͚औΔ
func(func()bool) 29
30 func(func(V)bool)
func(func(K, V)bool) 31
iterύοέʔδ
iterύοέʔδ 33 https://github.com/golang/go/tree/go1.23.0/src/iter 1.23ͰՃ͞Εͨ
PushΠςϨʔλ 34
PushΠςϨʔλ 35
PushΠςϨʔλ 36 iter.Seq ͷ༰Λશͯը໘ʹදࣔ͢Δྫ
PushΠςϨʔλ 37
PushΠςϨʔλ 38
PullΠςϨʔλ 39
PullΠςϨʔλ 40 • PushܕͷΠςϨʔλΛPullܕʹม͢Δ • next ΠςϨʔλͷ࣍ͷͱɺ༗ޮͳ͕͋Δ͔Ͳ͏͔ͷਅِΛฦ͢ • stop ΠςϨʔλΛऴྃͤ͞Δ
• stop Λݺͼग़ͨ͠ޙɺnext ͔Βθϩͱfalse͕ৗʹฦΔ
PullΠςϨʔλ 41
ඪ४Ξμϓλؔ
Adapter 43 ͷγʔέϯεΛϑΟϧλʔͯ͠৽͍͠γʔέϯεΛฦؔ͢
Adapter 44