Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
io.Writerで学ぶGoのインターフェース
Search
Daisuke Mino
August 14, 2018
Programming
0
250
io.Writerで学ぶGoのインターフェース
Daisuke Mino
August 14, 2018
Tweet
Share
More Decks by Daisuke Mino
See All by Daisuke Mino
複雑なコンポーネントで 状態をもつのをやめた話
minodisk
0
130
Goのエラーハンドリング
minodisk
0
200
Yet another side effect layer for Redux
minodisk
2
2.8k
Other Decks in Programming
See All in Programming
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
150
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 1
philipschwarz
PRO
0
110
全員アーキテクトで挑む、 巨大で高密度なドメインの紐解き方
agatan
8
11k
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
260
CloudNative Days Winter 2025: 一週間で作る低レイヤコンテナランタイム
ternbusty
7
1.8k
How Software Deployment tools have changed in the past 20 years
geshan
0
25k
[SF Ruby Conf 2025] Rails X
palkan
0
390
DSPy Meetup Tokyo #1 - はじめてのDSPy
masahiro_nishimi
1
110
All(?) About Point Sets
hole
0
230
JJUG CCC 2025 Fall: Virtual Thread Deep Dive
ternbusty
3
500
CSC305 Lecture 15
javiergs
PRO
0
210
Module Harmony
petamoriken
2
580
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
49
14k
Music & Morning Musume
bryan
46
7k
RailsConf 2023
tenderlove
30
1.3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
54k
How STYLIGHT went responsive
nonsquared
100
5.9k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.6k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Rails Girls Zürich Keynote
gr2m
95
14k
BBQ
matthewcrist
89
9.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.2k
Statistics for Hackers
jakevdp
799
230k
Agile that works and the tools we love
rasmusluckow
331
21k
Transcript
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