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の並行処理に入門しよう
Search
Shintaro Kanno
July 30, 2022
Programming
0
150
Goの並行処理に入門しよう
Shintaro Kanno
July 30, 2022
Tweet
Share
More Decks by Shintaro Kanno
See All by Shintaro Kanno
アトミックデザイン入門
shintaro8
0
300
gRPCミドルウェアを作ってみよう
shintaro8
1
590
Azure Functionsを使ってSlackに通知をしてみよう
shintaro8
0
540
Other Decks in Programming
See All in Programming
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 2
philipschwarz
PRO
0
130
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
160
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
3
840
これならできる!個人開発のすゝめ
tinykitten
PRO
0
140
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
460
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
470
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
430
CSC307 Lecture 01
javiergs
PRO
0
650
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
340
Cell-Based Architecture
larchanjo
0
160
Findy AI+の開発、運用におけるMCP活用事例
starfish719
0
2k
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
210
Featured
See All Featured
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
Making Projects Easy
brettharned
120
6.5k
HDC tutorial
michielstock
1
290
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.4k
The Curse of the Amulet
leimatthew05
0
6.7k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
2
76
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
99
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
0
1.1k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
770
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
250
The Pragmatic Product Professional
lauravandoore
37
7.1k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
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を!! ご清聴ありがとうございました