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
93
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
チャンネルを完全に理解する
Sendai.go2024年12月11日のLTしたい資料です。
Senoue
December 11, 2024
More Decks by Senoue
See All by Senoue
Go(5)分で! ECC暗号を動かして理解する BuriKaigi 2026
senoue
2
75
Goカードゲームを 作ってみた!
senoue
0
250
App_RunnerとRDSを活用したスケーラブルなWebAPI構築とインフラの自動化.pdf
senoue
1
150
Real-time Communication in Go with Melody and WebSockets
senoue
0
190
Adobeの生成AIのこと を調べてみた
senoue
0
230
ソフトウェア開発におけるAI :CopilotとGenie
senoue
0
230
Sendai.go x GDG Cloud 仙台 ハンズオン
senoue
0
84
GoでMecab
senoue
0
400
GKEとGoでエフェメラルなサービス
senoue
0
410
Other Decks in Programming
See All in Programming
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
190
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.5k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
540
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.4k
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
190
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
190
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
290
The Arts and Crafts of Work in the AI Era — Toward Mastery in Software Development
kuranuki
1
720
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
0
140
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
350
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
2
320
Featured
See All Featured
Game over? The fight for quality and originality in the time of robots
wayneb77
1
190
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
160
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
300
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
380
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
410
How to train your dragon (web standard)
notwaldorf
97
6.7k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
30 Presentation Tips
portentint
PRO
1
320
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