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
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
Transcript
18 slides
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を!! ご清聴ありがとうございました
More Decks by Shintaro Kanno
See All by Shintaro Kanno
アトミックデザイン入門
shintaro8
0
340
gRPCミドルウェアを作ってみよう
shintaro8
1
660
Azure Functionsを使ってSlackに通知をしてみよう
shintaro8
0
600
Other Decks in Programming
See All in Programming
Claude Code ハーネスDeep Dive – 君はどこまで制御する? -
takateeen_knj
6
5.3k
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
310
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
300
AI時代のTerminal環境を UNIX哲学で設計・運用する
yasunori0418
1
350
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
3
1.5k
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
170
LLM 101: Exploring LLMs, Starting with Tokens.
dalinaum
0
100
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
430
AWS CDKを理解するために公式の定義を深掘って解説してみる / Understanding AWS CDK by Digging into Its Official Definition
akihisaikeda
3
300
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
140
小さなアプリを作って公開するまで
onozaty
0
170
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
650
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Statistics for Hackers
jakevdp
799
230k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
630
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.2k
Why Our Code Smells
bkeepers
PRO
340
58k
The Limits of Empathy - UXLibs8
cassininazir
2
770
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
How to Talk to Developers About Accessibility
jct
2
620
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Music & Morning Musume
bryan
48
7.5k
So, you think you're a good person
axbom
PRO
2
2.3k