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
35
チャンネルを完全に理解する
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
45
Real-time Communication in Go with Melody and WebSockets
senoue
0
130
Adobeの生成AIのこと を調べてみた
senoue
0
170
ソフトウェア開発におけるAI :CopilotとGenie
senoue
0
160
Sendai.go x GDG Cloud 仙台 ハンズオン
senoue
0
48
GoでMecab
senoue
0
350
GKEとGoでエフェメラルなサービス
senoue
0
370
GAEのlogはStackDriverがいろいろやってくれている
senoue
1
540
GCPUG 仙台
senoue
1
440
Other Decks in Programming
See All in Programming
Serverless Rust: Your Low-Risk Entry Point to Rust in Production (and the benefits are huge)
lmammino
1
170
良いコードレビューとは
danimal141
9
8.5k
LINE messaging APIを使ってGoogleカレンダーと連携した予約ツールを作ってみた
takumakoike
0
130
もう少しテストを書きたいんじゃ〜 #phpstudy
o0h
PRO
21
4.4k
仕様変更に耐えるための"今の"DRY原則を考える
mkmk884
9
3.3k
「個人開発マネタイズ大全」が教えてくれたこと
bani24884
1
300
機能が複雑化しても 頼りになる FactoryBotの話
tamikof
1
250
Drawing Heighway’s Dragon- Recursive Function Rewrite- From Imperative Style in Pascal 64 To Functional Style in Scala 3
philipschwarz
PRO
0
160
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
760
ML.NETで始める機械学習
ymd65536
0
250
.NET Frameworkでも汎用ホストが使いたい!
tomokusaba
0
210
iOSでQRコード生成奮闘記
ktcryomm
2
140
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
Building Applications with DynamoDB
mza
93
6.2k
Side Projects
sachag
452
42k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
The Pragmatic Product Professional
lauravandoore
32
6.4k
Statistics for Hackers
jakevdp
797
220k
How to Ace a Technical Interview
jacobian
276
23k
Faster Mobile Websites
deanohume
306
31k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7.1k
The Cult of Friendly URLs
andyhume
78
6.2k
It's Worth the Effort
3n
184
28k
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