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
Daisuke Mino
August 14, 2018
Programming
260
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
io.Writerで学ぶGoのインターフェース
Daisuke Mino
August 14, 2018
More Decks by Daisuke Mino
See All by Daisuke Mino
複雑なコンポーネントで 状態をもつのをやめた話
minodisk
0
140
Goのエラーハンドリング
minodisk
0
220
Yet another side effect layer for Redux
minodisk
2
2.8k
Other Decks in Programming
See All in Programming
Verilogで学ぶCPU自作入門.pdf
uyuki234
1
390
setup-vp GitLab対応の裏側
naokihaba
0
120
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.1k
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
160
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
150
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
320
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.2k
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.9k
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
120
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
130
スマート反転とウェブアクセシビリティ
camiha
0
210
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
5
3.8k
Featured
See All Featured
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
700
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
420
Side Projects
sachag
456
43k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
330
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Optimizing for Happiness
mojombo
378
71k
Bash Introduction
62gerente
615
220k
GraphQLとの向き合い方2022年版
quramy
50
15k
RailsConf 2023
tenderlove
30
1.6k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
340
Crafting Experiences
bethany
1
350
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