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
io.Writerで学ぶGoのインターフェース
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Daisuke Mino
August 14, 2018
Programming
270
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
io.Writerで学ぶGoのインターフェース
Daisuke Mino
August 14, 2018
Transcript
20 slides
JP8SJUFSͰֶͿ ͷΠϯλʔϑΣʔε '0-*0ࣾ(Pݴޠษڧձ !NJOPEJTL
ΠϯλʔϑΣʔεͱ%VDL5ZQJOH JP8SJUFS ·ͱΊ ࣍
*OUFSGBDFBOE%VDL5ZQJOH ΠϯλʔϑΣʔεͱ%VDL5ZQJOH “If it walks like a duck and quacks
like a duck, it must be a duck.” 1
7 type Duck interface {¬ 8 ▸ Walk() int¬ 9
▸ Quack() string¬ 10 }¬ ΠϯλʔϑΣʔεͷఆٛํ๏
12 type RealDuck struct {¬ 13 ▸ current int¬ 14
}¬ 15 ¬ 16 func (d RealDuck) Walk() int {¬ 17 ▸ d.current += 1¬ 18 ▸ return d.current¬ 19 }¬ 20 ¬ 21 func (d RealDuck) Quack() string {¬ 22 ▸ return "Quack! Quack!"¬ 23 }¬ ࣮ͦͷ
25 type FakeDuck struct{}¬ 26 ¬ 27 func (d FakeDuck)
Walk() int {¬ 28 ▸ return 0¬ 29 }¬ 30 ¬ 31 func (d FakeDuck) Quack() string {¬ 32 ▸ return "Bow! Wow!"¬ 33 }¬ ࣮ͦͷ
35 func main() {¬ 36 ▸ r := RealDuck{}¬ 37
▸ Walk3StepsAndQuack(r)¬ 38 ¬ 39 ▸ f := FakeDuck{}¬ 40 ▸ Walk3StepsAndQuack(f)¬ 41 }¬ 42 ¬ 43 func Walk3StepsAndQuack(d Duck) {¬ 44 ▸ d.Walk()¬ 45 ▸ d.Walk()¬ 46 ▸ d.Walk()¬ 47 ▸ fmt.Println(d.Quack())¬ 48 }¬ ΠϯλʔϑΣʔεΛҾʹͱΔؔ
JP8SJUFS JP8SJUFS CZUFྻΛॻ͖ࠐΉͨΊͷΠϯλʔϑΣʔεͰ සग़͢ΔύοέʔδΛͬͯ۩ମྫͱڞʹ ΠϯλʔϑΣʔεΛֶͿ 2
90 type Writer interface {¬ 91 ▸ Write(p []byte) (n
int, err error)¬ 92 }¬ JP8SJUFS JPJPHP--
֎͔Βϩάͷग़ྗઌΛม͑ΒΕΔ Α͏ͳ࣮
46 type BullBear struct {¬ 47 ▸ Logger io.Writer¬ 48
}¬ 49 ¬ 50 func (b BullBear) DoSomething() error {¬ 51 ▸ log := []byte("did something\n")¬ 52 ▸ if _, err := b.Logger.Write(log); err != nil {¬ 53 ▸ ▸ return err¬ 54 ▸ }¬ 55 ▸ return nil¬ 62 }¬ JP8SJUFSͳ-PHHFSΛͬͨTUSVDUΛ࡞Δ
17 func logToStdout() error {¬ 18 ▸ b := BullBear{os.Stdout}¬
19 ▸ return b.DoSomething()¬ 20 }¬ ඪ४ग़ྗʹϩάΛग़ྗ͢Δ
22 func logToFile() error {¬ 23 ▸ file, err :=
os.Create("test.log")¬ 24 ▸ if err != nil {¬ 25 ▸ ▸ return err¬ 26 ▸ }¬ 27 ▸ b := BullBear{file}¬ 28 ▸ return b.DoSomething()¬ 29 }¬ ϑΝΠϧʹϩάΛग़ྗ͢Δ
ඪ४ग़ྗʹϑΝΠϧʹ ϩάΛग़ྗͨ͘͠Ͷʁ
JP.VMUJ8SJUFS
31 func logToStdoutAndFile() error {¬ 32 ▸ file, err :=
os.Create("test.log")¬ 33 ▸ if err != nil {¬ 34 ▸ ▸ return err¬ 35 ▸ }¬ 36 ▸ mw := io.MultiWriter(os.Stdout, file)¬ 37 ▸ b := BullBear{mw}¬ 38 ▸ return b.DoSomething()¬ 39 }¬ ඪ४ग़ྗͱϑΝΠϧʹϩάΛग़ྗ͢Δ
10 func TestLogging(t *testing.T) {¬ 11 ▸ buf := bytes.NewBuffer([]byte{})¬
12 ▸ b := writer.BullBear{Logger: buf}¬ 13 ▸ b.DoSomething()¬ 14 ▸ if buf.String() != "did something\n" {¬ 15 ▸ ▸ t.Error("wrong log")¬ 16 ▸ }¬ 17 }¬ ϩάग़ྗΛςετ͢Δ
3 ·ͱΊ
w JOUFSGBDFܕ w %VDL5ZQJOHͰϙϦϞʔϑΟζϜΛ͍ͬͯ͘
None
More Decks by Daisuke Mino
See All by Daisuke Mino
複雑なコンポーネントで 状態をもつのをやめた話
minodisk
0
150
Goのエラーハンドリング
minodisk
0
230
Yet another side effect layer for Redux
minodisk
2
2.8k
Other Decks in Programming
See All in Programming
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
340
Skills Are the New npm [AAIF Bengaluru, Nov 2026]
shivamsaraswat
0
140
仕様駆動開発入門 (何を作るかを先に決めて渡す) | 目指せ Kiro マスター 05
inariku
PRO
3
240
Stage 3でも仕様は変わる。アプリケーション開発者のためのTC39プロセスの読み方
bicstone
1
930
眠らない開発チームの作り方
biwakonbu
0
170
AIエージェントの「テストは通るのに本番で壊れる」を減らす
shibuiwilliam
3
130
あなたはMCPの機能を、まだ知らない。
4geru
1
740
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
170
はじめてのKubernetesコントリビューション
nasa9084
0
330
ruby/uri meets WHATWG
jinroq
0
170
Canvas 2D Context Next
petamoriken
0
690
Scalaの型でマルチテナントの認可を守る
nextbeatdev
0
180
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
The World Runs on Bad Software
bkeepers
PRO
72
12k
The Curse of the Amulet
leimatthew05
4
17k
The SEO Collaboration Effect
kristinabergwall1
1
630
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
440
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
970
Designing Experiences People Love
moore
143
24k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
610
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
830