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
チャンネルを完全に理解する
Search
Senoue
December 11, 2024
Programming
0
55
チャンネルを完全に理解する
Sendai.go2024年12月11日のLTしたい資料です。
Senoue
December 11, 2024
Tweet
Share
More Decks by Senoue
See All by Senoue
App_RunnerとRDSを活用したスケーラブルなWebAPI構築とインフラの自動化.pdf
senoue
1
79
Real-time Communication in Go with Melody and WebSockets
senoue
0
150
Adobeの生成AIのこと を調べてみた
senoue
0
190
ソフトウェア開発におけるAI :CopilotとGenie
senoue
0
190
Sendai.go x GDG Cloud 仙台 ハンズオン
senoue
0
52
GoでMecab
senoue
0
370
GKEとGoでエフェメラルなサービス
senoue
0
390
GAEのlogはStackDriverがいろいろやってくれている
senoue
1
550
GCPUG 仙台
senoue
1
460
Other Decks in Programming
See All in Programming
Android端末で実現するオンデバイスLLM 2025
masayukisuda
1
100
為你自己學 Python - 冷知識篇
eddie
1
350
Deep Dive into Kotlin Flow
jmatsu
1
270
rage against annotate_predecessor
junk0612
0
160
MCPで実現するAIエージェント駆動のNext.jsアプリデバッグ手法
nyatinte
7
1.1k
Navigating Dependency Injection with Metro
zacsweers
3
200
ぬるぬる動かせ! Riveでアニメーション実装🐾
kno3a87
1
200
Laravel Boost 超入門
fire_arlo
2
210
ファインディ株式会社におけるMCP活用とサービス開発
starfish719
0
270
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
140
TDD 実践ミニトーク
contour_gara
1
290
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
7
3.3k
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
520
Why Our Code Smells
bkeepers
PRO
339
57k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Building Adaptive Systems
keathley
43
2.7k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Making the Leap to Tech Lead
cromwellryan
135
9.5k
The Art of Programming - Codeland 2020
erikaheidi
55
13k
Producing Creativity
orderedlist
PRO
347
40k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Transcript
Goのチャネル を完全に理解する 瀬上祐匡 2024/12/11
瀬上 祐匡(せのうえ ひろまさ) 株式会社クラウドスミス テクニカルマネージャー • AWS,GCP, Go, Python等,BI,データ分析 •
@senoue,@hiromasa.senoue • モノノフです。 • 好きなものは、Cloud Function • Sendai.go やってます • TynyGo-keebなど 自己紹介 株式会社クラウドスミス 仙台を拠点とした、Webシステム中心の開発会社です。
- 定義: - チャンネルは、 Goroutine間でデータを送受信 するためのパイプです。 - 特徴: - 型指定される。
- 複数のGoroutineから安全にデータを共有で きる同期機構がある。 1. チャンネルとは?
- a. 宣言と作成 - - b. 送信と受信 - c. バッファ付きチャンネル
2. チャンネルの基本操作 ch := make(chan int) // 整数 ch <- 10 // 送信 value := <-ch // 受信 ch := make(chan int, 3) // バッファサイズ 3 のチャンネル
- a. Goroutine間通信 - 3. 実践的な使用例 func worker(id int, ch
chan int) { for n := range ch { fmt.Printf("Worker %d received %d\n", id, n) } } func main() { ch := make(chan int) for i := 0; i < 3; i++ { go worker(i, ch) } for i := 0; i < 5; i++ { ch <- i } close(ch) } 実際の動作:https://go.dev/play/p/K54rceX7AUn
- b. select を用いた多重チャネル操作 - 3. 実践的な使用例 func main() {
ch1 := make(chan string) ch2 := make(chan string) go func() { ch1 <- "from channel 1" }() go func() { ch2 <- "from channel 2" }() for i := 0; i < 2; i++ { select { case msg1 := <-ch1: fmt.Println(msg1) case msg2 := <-ch2: fmt.Println(msg2) } } }
- デッドロック : 送られた値を誰も受け取らない場 合、または相手がいない場合に発生。 - Channelの適切なクローズ (close) が必要。 -
Goroutineリーク: 終了条件なく走り続ける Goroutineを避ける。 4. 注意点とベストプラクティス
- デモ 4. 注意点とベストプラクティス
まとめ
- チャネルによって、ゴルーチン間の通信や同期がシ ンプルになります。 明示的なロック機構( sync.Mutexなど)を使用せず に済むため、コードがわかりやすくなります。 - チャネルを使ってタスクをキューに入れることがで き、ワーカーゴルーチンで並列に処理することができ ます。
これにより、負荷の高いタスクの分散処理が容易に なります。 5. メリット
- バッファなしチャンネルとバッファありチャンネルの 違いは? - バッファなしは同期的、バッファありは非同期 的に動作できます。 ただし、バッファがいっぱいになるとブロックさ れます。 - なぜチャンネルを使うべきなのか?
- 安全にデータをやりとりし、同期させるために 有用だからです。 5. まとめ
Thank You