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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Senoue
December 11, 2024
Programming
0
85
チャンネルを完全に理解する
Sendai.go2024年12月11日のLTしたい資料です。
Senoue
December 11, 2024
Tweet
Share
More Decks by Senoue
See All by Senoue
Go(5)分で! ECC暗号を動かして理解する BuriKaigi 2026
senoue
2
60
Goカードゲームを 作ってみた!
senoue
0
200
App_RunnerとRDSを活用したスケーラブルなWebAPI構築とインフラの自動化.pdf
senoue
1
140
Real-time Communication in Go with Melody and WebSockets
senoue
0
180
Adobeの生成AIのこと を調べてみた
senoue
0
220
ソフトウェア開発におけるAI :CopilotとGenie
senoue
0
220
Sendai.go x GDG Cloud 仙台 ハンズオン
senoue
0
76
GoでMecab
senoue
0
390
GKEとGoでエフェメラルなサービス
senoue
0
410
Other Decks in Programming
See All in Programming
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
340
AWS×クラウドネイティブソフトウェア設計 / AWS x Cloud-Native Software Design
nrslib
11
2.6k
どんと来い、データベース信頼性エンジニアリング / Introduction to DBRE
nnaka2992
1
240
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
1
130
ふつうの Rubyist、ちいさなデバイス、大きな一年
bash0c7
0
710
Windows on Ryzen and I
seosoft
0
210
nuget-server - あなたが必要だったNuGetサーバー
kekyo
PRO
0
200
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
420
Codexに役割を持たせる 他のAIエージェントと組み合わせる実務Tips
o8n
3
1.1k
atmaCup #23でAIコーディングを活用した話
ml_bear
4
750
PostgreSQL を使った快適な go test 環境を求めて
otakakot
0
470
2026/02/04 AIキャラクター人格の実装論 口 調の模倣から、コンテキスト制御による 『思想』と『行動』の創発へ
sr2mg4
0
720
Featured
See All Featured
Making Projects Easy
brettharned
120
6.6k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
140
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Designing for Performance
lara
611
70k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Six Lessons from altMBA
skipperchong
29
4.2k
Google's AI Overviews - The New Search
badams
0
930
New Earth Scene 8
popppiees
1
1.7k
So, you think you're a good person
axbom
PRO
2
1.9k
KATA
mclloyd
PRO
35
15k
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