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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Shintaro Kanno
July 30, 2022
Programming
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Goの並行処理に入門しよう
Shintaro Kanno
July 30, 2022
More Decks by Shintaro Kanno
See All by Shintaro Kanno
アトミックデザイン入門
shintaro8
0
330
gRPCミドルウェアを作ってみよう
shintaro8
1
650
Azure Functionsを使ってSlackに通知をしてみよう
shintaro8
0
600
Other Decks in Programming
See All in Programming
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
0
280
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
250
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
330
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
320
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
120
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.8k
一人だけ、Kiroが静止する日
hideg
0
110
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
330
A2UI for Android: Safely Rendering AI-Generated UI with Jetpack Compose - DroidKaigi 2026
itsmedreamwalker
0
140
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
620
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
3
350
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
1
550
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
690
Are puppies a ranking factor?
jonoalderson
2
3.9k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Building Adaptive Systems
keathley
44
3.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Code Reviewing Like a Champion
maltzj
528
40k
Utilizing Notion as your number one productivity tool
mfonobong
4
590
Designing for Performance
lara
611
70k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Transcript
Goの並行処理に入門しよう
自己紹介
- 官野 慎太朗 (かんの しんたろう) - 都内のIT事業会社にて勤務 - 手術支援ロボットPF開発 -
土木建機PF開発 - 技術経験 - 言語 - JavaScript/TypeScript - Go - DB - MySQL, PostgreSQL - コンテナ関係 - Docker - Kubernetes - クラウド技術 - AWS, Azure - その他 - Git, REST API, gRPC, Concourse, Flyway, etc… GitHub : @shinshin8 Medium : @doctorkanno572
Goを始めると出会う言葉たち Goroutine Channel Interface Struct ダックタイピング
アジェンダ 1. Goroutineって何? 2. Channelって何? 3. 終わりに
Goroutineって何?
Goroutineとは.... “A goroutine is a lightweight thread managed by the
Go runtime.” - A Tour of Go : Goroutine 邦訳:GoroutineとはGoのランタイムで管理される軽量のスレッド
とはいえ、パッとイメージが浮かばない。。。
package main import ( "log" "time" ) func say(s string)
{ for i := 0; i < 5; i++ { time.Sleep(1 * time.Second) log.Println(s) } } func main() { go say("world") say("hello") } package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } 元のコード 手を加えたコード 出典:A Tour of Go : Goroutine
大体の処理の流れのイメージ $ go run main.go 2022/07/29 22:07:43 hello 2022/07/29 22:07:43
world 2022/07/29 22:07:44 hello 2022/07/29 22:07:44 world 2022/07/29 22:07:45 world 2022/07/29 22:07:45 hello 2022/07/29 22:07:46 hello 2022/07/29 22:07:46 world 2022/07/29 22:07:47 world 2022/07/29 22:07:47 hello
ちなみに・・・・ package main import ( "log" // "time" ★ コメントアウト
) func say(s string) { for i := 0; i < 5; i++ { // time.Sleep(1 * time.Second) ★ コメントアウト log.Println(s) } } func main() { go say("world") say("hello") } —---------------------------------------------------------- $ go run main.go 2022/07/29 22:35:31 hello 2022/07/29 22:35:31 hello 2022/07/29 22:35:31 hello 2022/07/29 22:35:31 hello 2022/07/29 22:35:31 hello
Channelって何?
Channelとは.... • Goroutine間での値のやりとりを可能にするパイプのようなもの。 • make(chan <扱いたい値の型>) でchannelを定義 • channel <-
で定義したchannelに値を代入 • <- channel で定義したchannelから値を取得
channelを使ったgoroutine間のやりとり func main() { flg := make(chan bool) ★bool型のchannelを定義 go
another(flg) time.Sleep(time.Second * 5) flg <- true ★ channelにtrueを代入 } func another(flg chan bool) { for { time.Sleep(time.Second * 2) select { case <-flg: ★channelがtrueの場合 log.Println("goroutine is done") return default: log.Println("goroutine is running") } } } $ go run main.go 2022/07/30 17:30:34 goroutine is running 2022/07/30 17:30:36 goroutine is running 2022/07/30 17:30:38 goroutine is done 実行結果
channelを使ったgoroutine間のやりとりのイメージ
終わりに
どういった場面で並行処理が使われるのか? • 処理実行のパフォーマンス改善 ◦ Syncパッケージを使った同期や排他制御 • Webフレームワーク、gRPCを使用する際の理解 ◦ contextを用いたgoroutineの扱い
楽しいGo Lifeを!! ご清聴ありがとうございました