SlideShare a Scribd company logo
1 of 11
Go言語でのステート管理
© 2015 sprout Inc. All Rights Reserved.
1
株式会社スプラウト
高橋 明
Twitter : @Talos208
HTTPは(基本)Stateless
• 1回のHTTPリクエスト+レスポンスで完結している
• 一連のアクセスかどうか、HTTPではチェックできない
• cookieなどを使って、間接的にステート管理
• HTTPでなければ、プロトコル上でステート管理できる
• HTTPでも、WebSocket/WebRTCなら可能
• その際の手法はHTTP+cookieとは異なる
© 2015 sprout Inc. All Rights Reserved.
2
FSMによるステート管理
© 2015 sprout Inc. All Rights Reserved.
3
• Finite State Machine
有限個の状態からなるステートマシン
開始 状態0 状態1 状態2
トリガー0 状態0
トリガー1 状態1
トリガー2 状態1
トリガー3 状態2
トリガー4 状態2
トリガー5 状態1
トリガー6 終了
状態遷移図 状態遷移表
gotoによる実装
func main() {
Start:
// 開始状態の処理
if trigger0() {
goto State0
}
goto Start
State0:
// 状態0の処理
if trigger1() {
goto State1
} else if trigger4() {
goto State2
}
goto State0
State1:
.
.
.
© 2015 sprout Inc. All Rights Reserved.
4
Pros & Cons
• Pros
• 処理そのままの形で書け、記述量が少ない
• FSMにあるどのような遷移でも記述できる
• Cons
• FSMが大きくなると見通しが悪い
• 共通処理やデータの管理が大変
© 2015 sprout Inc. All Rights Reserved.
5
4.Tail call eliminationを利用
• 手続き末尾での手続き呼び出し
• 手続きからの戻り値を自身の戻り値にする
• 上記を満たす手続き呼び出しをTail callと呼び、単純なジャン
プに最適化可能
© 2015 sprout Inc. All Rights Reserved.
6
• これを利用して、関数のsyntaxでgotoを書ける
• ただし、最適化が行われるかどうかは、処理系依存
関数コール
関数コール
リターン
リターン
関数コール
関数コール
リターン
リターン
Tail Callを使用した実装
func Start() {
// 初期状態の処理
switch {
case trigger0():
State0()
default:
Start()
}
}
func State0() {
// 状態0の処理
switch {
case trigger1():
State1()
case trigger4():
State2()
default:
State0()
}
}
© 2015 sprout Inc. All Rights Reserved.
7
func State1() {
// 状態1の処理
switch {
case trigger2():
State1()
case trigger3():
State2()
default:
State1()
}
}
.
.
.
func main() {
Start()
}
Goではtail call eliminationされるか
main.Start:
0000000000002000 movq %gs:0x000008a0,%rcx
0000000000002009 cmpq 0x10(%rcx),%rsp
000000000000200d ja 0x00002016
000000000000200f callq runtime.morestack_noctxt
0000000000002014 jmp main.Start
0000000000002016 movq $0x00000001,%rax
000000000000201d cmpb $0x00,%al
000000000000201f je 0x00002027
0000000000002021 callq main.State1
0000000000002026 ret
0000000000002027 callq main.Start
000000000000202c jmp 0x00002026
000000000000202e addb %al,(%rax)
© 2015 sprout Inc. All Rights Reserved.
8
スタックが足りな
ければ伸長
trigger0()をコール(最適化)
State1をコール
自身(Start)をコール
リターン
リターン
• ……されていない
• 実行するとスタックを食いつぶす
goroutineの利用
© 2015 sprout Inc. All Rights Reserved.
9
func Start(q chan bool) {
// 初期状態の処理
switch {
case trigger0():
go State0(q)
default:
go Start(q)
}
}
func State0(q chan bool) {
// 状態0の処理
switch {
case trigger1():
go State1(q)
case trigger4():
go State2(q)
default:
go State0(q)
}
}
func State1(q chan bool) {
// 状態0の処理
switch {
case trigger2():
go State1(q)
case trigger3():
go State2(q)
default:
go State1(q)
}
}
.
.
.
func main() {
q := make(chan bool)
go Start(q)
<-q
}
Pros & Cons
• Pros
• GoでTailCallっぽく書いてもスタックを消費していかない
• 「go ××」という記述が遷移っぽい
• 複数のFSMが同時に動く状態を自然に書ける
• Cons
• main()と並列に動いてしまうので、終了を待ち受けないとならない
• goroutineの呼び忘れ/多重呼び出しのバグがありうる
• 実際にやらかしました……
• CPU負荷、メモリ消費量が少々増える
© 2015 sprout Inc. All Rights Reserved.
10
まとめ
• HTTPでない場合、ステート管理が必要になることがある
• ステート管理手法の一つにFSMがある
• Go言語では複数の書き方がある
• goto
• TailCall(将来の最適化に期待?)
• goroutine
• 他にも
• for + switch
• State Pattern
• 目的に応じて使い分けよう
© 2015 sprout Inc. All Rights Reserved.
11

More Related Content

Recently uploaded

デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)UEHARA, Tetsutaro
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineerYuki Kikuchi
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...博三 太田
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfFumieNakayama
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案sugiuralab
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfFumieNakayama
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成Hiroshi Tomioka
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?akihisamiyanaga1
 

Recently uploaded (9)

デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版) 2024年4月作成
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Go言語でのステート管理

  • 1. Go言語でのステート管理 © 2015 sprout Inc. All Rights Reserved. 1 株式会社スプラウト 高橋 明 Twitter : @Talos208
  • 2. HTTPは(基本)Stateless • 1回のHTTPリクエスト+レスポンスで完結している • 一連のアクセスかどうか、HTTPではチェックできない • cookieなどを使って、間接的にステート管理 • HTTPでなければ、プロトコル上でステート管理できる • HTTPでも、WebSocket/WebRTCなら可能 • その際の手法はHTTP+cookieとは異なる © 2015 sprout Inc. All Rights Reserved. 2
  • 3. FSMによるステート管理 © 2015 sprout Inc. All Rights Reserved. 3 • Finite State Machine 有限個の状態からなるステートマシン 開始 状態0 状態1 状態2 トリガー0 状態0 トリガー1 状態1 トリガー2 状態1 トリガー3 状態2 トリガー4 状態2 トリガー5 状態1 トリガー6 終了 状態遷移図 状態遷移表
  • 4. gotoによる実装 func main() { Start: // 開始状態の処理 if trigger0() { goto State0 } goto Start State0: // 状態0の処理 if trigger1() { goto State1 } else if trigger4() { goto State2 } goto State0 State1: . . . © 2015 sprout Inc. All Rights Reserved. 4
  • 5. Pros & Cons • Pros • 処理そのままの形で書け、記述量が少ない • FSMにあるどのような遷移でも記述できる • Cons • FSMが大きくなると見通しが悪い • 共通処理やデータの管理が大変 © 2015 sprout Inc. All Rights Reserved. 5
  • 6. 4.Tail call eliminationを利用 • 手続き末尾での手続き呼び出し • 手続きからの戻り値を自身の戻り値にする • 上記を満たす手続き呼び出しをTail callと呼び、単純なジャン プに最適化可能 © 2015 sprout Inc. All Rights Reserved. 6 • これを利用して、関数のsyntaxでgotoを書ける • ただし、最適化が行われるかどうかは、処理系依存 関数コール 関数コール リターン リターン 関数コール 関数コール リターン リターン
  • 7. Tail Callを使用した実装 func Start() { // 初期状態の処理 switch { case trigger0(): State0() default: Start() } } func State0() { // 状態0の処理 switch { case trigger1(): State1() case trigger4(): State2() default: State0() } } © 2015 sprout Inc. All Rights Reserved. 7 func State1() { // 状態1の処理 switch { case trigger2(): State1() case trigger3(): State2() default: State1() } } . . . func main() { Start() }
  • 8. Goではtail call eliminationされるか main.Start: 0000000000002000 movq %gs:0x000008a0,%rcx 0000000000002009 cmpq 0x10(%rcx),%rsp 000000000000200d ja 0x00002016 000000000000200f callq runtime.morestack_noctxt 0000000000002014 jmp main.Start 0000000000002016 movq $0x00000001,%rax 000000000000201d cmpb $0x00,%al 000000000000201f je 0x00002027 0000000000002021 callq main.State1 0000000000002026 ret 0000000000002027 callq main.Start 000000000000202c jmp 0x00002026 000000000000202e addb %al,(%rax) © 2015 sprout Inc. All Rights Reserved. 8 スタックが足りな ければ伸長 trigger0()をコール(最適化) State1をコール 自身(Start)をコール リターン リターン • ……されていない • 実行するとスタックを食いつぶす
  • 9. goroutineの利用 © 2015 sprout Inc. All Rights Reserved. 9 func Start(q chan bool) { // 初期状態の処理 switch { case trigger0(): go State0(q) default: go Start(q) } } func State0(q chan bool) { // 状態0の処理 switch { case trigger1(): go State1(q) case trigger4(): go State2(q) default: go State0(q) } } func State1(q chan bool) { // 状態0の処理 switch { case trigger2(): go State1(q) case trigger3(): go State2(q) default: go State1(q) } } . . . func main() { q := make(chan bool) go Start(q) <-q }
  • 10. Pros & Cons • Pros • GoでTailCallっぽく書いてもスタックを消費していかない • 「go ××」という記述が遷移っぽい • 複数のFSMが同時に動く状態を自然に書ける • Cons • main()と並列に動いてしまうので、終了を待ち受けないとならない • goroutineの呼び忘れ/多重呼び出しのバグがありうる • 実際にやらかしました…… • CPU負荷、メモリ消費量が少々増える © 2015 sprout Inc. All Rights Reserved. 10
  • 11. まとめ • HTTPでない場合、ステート管理が必要になることがある • ステート管理手法の一つにFSMがある • Go言語では複数の書き方がある • goto • TailCall(将来の最適化に期待?) • goroutine • 他にも • for + switch • State Pattern • 目的に応じて使い分けよう © 2015 sprout Inc. All Rights Reserved. 11