Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Go1.13以後のエラーハンドリングについて語ろう / Let's talk about ...
Search
Koya IWAMURA
October 28, 2019
Programming
8.9k
10
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Go1.13以後のエラーハンドリングについて語ろう / Let's talk about error handling after Go 1 13
Koya IWAMURA
October 28, 2019
More Decks by Koya IWAMURA
See All by Koya IWAMURA
What's new in Go 1.27?
ciarana
1
590
What's new in Go 1.26?
ciarana
2
520
What's new in Go 1.25?
ciarana
1
130
What's new in Go 1.24?
ciarana
1
300
What's new in Go 1.21?
ciarana
2
1.8k
PGOによるコンパイラ最適化 / Compiler Optimization with PGO
ciarana
2
1.1k
Other Decks in Programming
See All in Programming
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
11
8.3k
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
280
Kotlinの15年とServer-Side Kotlin
n_takehata
1
170
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.4k
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
1.5k
phperだってパンチカードでコーディングしたい
o0h
PRO
0
460
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3k
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
180
クラウドゲーミングのパフォーマンス最適化に挑む / Cloud Gaming Performance Tuning
vivion
0
260
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
200
残高管理から台帳サービスへの進化
artoy
1
210
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
150
Featured
See All Featured
Practical Orchestrator
shlominoach
192
12k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
My Coaching Mixtape
mlcsv
0
360
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
600
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Deep Space Network (abreviated)
tonyrice
0
380
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
610
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
Transcript
Go1.13 以後の エラーハンドリング について語ろう Go Conference 2019 Autumn, Oct 28
2019 Koya IWAMURA CyberAgent, Inc. @cia_rana
Go1.13 からスタックトレースをとるための機能が追加 • fmt.Errorf (%w) • errors.Is • errors.As •
errors.Unwrap
fmt.Errorf (%w) • 既存のエラーをラッピング • 情報を付加 func MyFunc() error {
err := f() return fmt.Errorf(“MyFunc: %w”, err) }
errors.Is • 値の比較 if err == sql.ErrNoRows { fmt.Println(err) }
if errors.Is(err, sql.ErrNoRows) { fmt.Println(err) } Go1.13〜
errors.As • 型の比較(assertion, switch)、キャプチャ if e, ok := err.(*url.Error); ok
{ fmt.Printf(“isTimeout: %v”, e.Timeout()) } var e *url.Error if errors.As(err, &e) { fmt.Printf(“isTimeout: %v”, e.Timeout()) } Go1.13〜
Go1.13以後 エラーハンドリングする上での留意点
01 `if err != nil` が 無力化される Go1.13以後 エラーハンドリングする上での留意点
`if err != nil` が無力化される func f() ([]byte, error) {
resp, err := http.Get("cyberagent.ai") if err != nil { return nil, err } return ioutil.ReadAll(resp.Body) } func main() { a, err := f() if err != nil { fmt.Println(err) } fmt.Println(string(a)) }
`if err != nil` が無力化される func f() ([]byte, error) {
resp, err := http.Get("cyberagent.ai") if err != nil { return nil, err } a, err := ioutil.ReadAll(resp.Body) return a, fmt.Errorf(“%w”, err) } func main() { a, err := f() if err != nil { fmt.Println(err) } fmt.Println(string(a)) }
`if err != nil` が無力化される func f() ([]byte, error) {
resp, err := http.Get("cyberagent.ai") if err != nil { return nil, err } a, err := ioutil.ReadAll(resp.Body) return a, fmt.Errorf(“%w”, err) } func main() { a, err := f() if err != nil { fmt.Println(err) } fmt.Println(string(a)) } errがnilでも・・・
`if err != nil` が無力化される func f() ([]byte, error) {
resp, err := http.Get("cyberagent.ai") if err != nil { return nil, err } a, err := ioutil.ReadAll(resp.Body) return a, fmt.Errorf(“%w”, err) } func main() { a, err := f() if err != nil { fmt.Println(err) } fmt.Println(string(a)) } errがnilでも・・・ if文の中が走っちゃう
`if err != nil` が無力化される func f() ([]byte, error) {
resp, err := http.Get("cyberagent.ai") if err != nil { return nil, err } a, err := ioutil.ReadAll(resp.Body) return a, fmt.Errorf(“%w”, err) } func main() { a, err := f() if errors.Is(err, nil) { fmt.Println(err) } fmt.Println(string(a)) } errors.Isを使おう!
02 xerrorsと同じ挙動に 見せかけて実は違う Go1.13以後 エラーハンドリングする上での留意点
xerrorsと同じ挙動に見せかけて実は違う https://github.com/golang/go/wiki/ErrorValueFAQ
xerrorsと同じ挙動に見せかけて実は違う xerrors.Errorfでは ラップした位置に関する情報をとれる
xerrorsと同じ挙動に見せかけて実は違う fmt.Errorf では ラップした位置に関する情報をとれない
xerrorsと同じ挙動に見せかけて実は違う xerrors.Errorf で error をラップするには フォーマット指示子を ”: %w” にして 末尾に持ってこなくてはならない
xerrors.Errorf(“MyError: %w”, err)
xerrorsと同じ挙動に見せかけて実は違う fmt.Errorf で error をラップするには フォーマット指示子が ”%w” でありさえすれば良い fmt.Errorf(“MyError: %w
in main.main”, err)
Thank you for listening!