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
110
Goの並行処理に入門しよう
Shintaro Kanno
July 30, 2022
Tweet
Share
More Decks by Shintaro Kanno
See All by Shintaro Kanno
アトミックデザイン入門
shintaro8
0
210
gRPCミドルウェアを作ってみよう
shintaro8
1
320
Azure Functionsを使ってSlackに通知をしてみよう
shintaro8
0
390
Other Decks in Programming
See All in Programming
Elm Form Validation
bkuhlmann
0
510
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
200
エンターテイメント業界で利用されるAWS
demuyan
0
210
Code Reviews
bkuhlmann
4
890
Netty Chicago Java User Group 2024-04-17
sullis
0
170
ゆるい個人開発のススメ
kuroppe1819
10
980
try! Swift Tokyo 初参加報告LT
hinakko2
0
220
Changed Rules: Architectures with Lightweight Stores
manfredsteyer
PRO
0
240
冗長なエラーログを削減し、スタックトレースを手に入れる / Reducing Verbose Error Logs and Obtaining Stack Traces
upamune
0
410
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
GitHub Actionsで泣かないためにやっておきたい設定 / Recommended GHA settings to avoid crying
pinkumohikan
3
530
Ruby GitHub Packages
bkuhlmann
0
630
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
659
120k
Designing Experiences People Love
moore
136
23k
The Invisible Side of Design
smashingmag
294
49k
The MySQL Ecosystem @ GitHub 2015
samlambert
243
12k
Faster Mobile Websites
deanohume
299
30k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
Become a Pro
speakerdeck
PRO
11
4.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
4 Signs Your Business is Dying
shpigford
175
21k
It's Worth the Effort
3n
180
27k
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を!! ご清聴ありがとうございました