Slide 1

Slide 1 text

40-*%JO(P Go (Un) Conference #5

Slide 2

Slide 2 text

I’m @syossan27 Golang Beginner
 
 ʕ ◔ϖ◔ʔ < Write simple code !

Slide 3

Slide 3 text

࠷ۙ࡞ͬͨ΋ͷ

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

SOLIDʁ OOPʹ͓͚Δ̑ͭͷઃܭݪଇ ɾ୯Ұ੹຿ͷݪଇ ɾ։์ด࠯ͷݪଇ ɾϦείϑஔ׵ݪଇ ɾΠϯλʔϑΣʔε෼཭ͷݪଇ ɾґଘੑٯసͷݪଇ

Slide 6

Slide 6 text

Ref.

Slide 7

Slide 7 text

୯Ұ੹຿ͷݪଇ ̍մͷίʔυʢϞδϡʔϧɺΫϥεʣʹରͯ͠ ̍ͭͷมߋཧ༝Λ࣋ͭ͜ͱɻ 㱺 ґଘίʔυʹΑΔ࿈࠯తมߋͷ༧๷

Slide 8

Slide 8 text

In Go Goʹ͸Ϋϥεͱ͍͏֓೦͕ແ͍ͨΊɺ͜͜Ͱ ͷҰմͷίʔυ͸ύοέʔδͷ֓೦Ͱݴ͍׵ ͑Δ͜ͱ͕ग़དྷΔɻ 㱺ύοέʔδ͕୯Ұͷ੹຿Λ୲͏Α͏ߟྀ͢ Δ

Slide 9

Slide 9 text

In Go Goʹ͸Ϋϥεͱ͍͏֓೦͕ແ͍ͨΊɺ͜͜Ͱ ͷҰմͷίʔυ͸ύοέʔδͷ֓೦Ͱݴ͍׵ ͑Δ͜ͱ͕ग़དྷΔɻ 㱺ύοέʔδ͕୯Ұͷ੹຿Λ୲͏Α͏ߟྀ͢ Δ 㱺ద੾ͳύοέʔδ໊ͷ໋໊͕ॏཁ

Slide 10

Slide 10 text

Bad Package name ɾcommon ɾutils ɾprivate etc…

Slide 11

Slide 11 text

Why bad? ໊લ͔Βͯ͠ଟ͘ͷ੹೚Λഎෛ͍ࠐΜͰ͍Δ ͨΊɺมߋཧ༝Λଟ๊͑͘ࠐΉՄೳੑ͕ߴ͍

Slide 12

Slide 12 text

Good name ɾnet/http ɾos/exec ɾencoding/json etc…

Slide 13

Slide 13 text

։์ด࠯ͷݪଇ ̍մͷίʔυʢϞδϡʔϧɺΫϥεʣ͕ ɾ֦ுʹରͯ͠։์͞Ε͍ͯΔ ɾमਖ਼ʹରͯ͠ด࠯͞Ε͍ͯΔ ঢ়ଶʹͳΔ͜ͱΛ໨ࢦ͢ɻ 㱺 ίʔυมߋແ͠ʹػೳमਖ਼ɾ௥Ճ͕Մೳ

Slide 14

Slide 14 text

Example package main import "fmt" type Cat struct{} func (c Cat) Legs() int { return 4 } func (c Cat) PrintLegs() { fmt.Printf("I have %d legs\n”, c.Legs()) } func main() { var cat Cat cat.PrintLegs() }

Slide 15

Slide 15 text

Try ৽͘͠6ຊ଍ͷOctCatͷ֓೦͕ඞཁʹͳͬͨ ৔߹ʹͲ͏ରԠ͢Δ͔ʁ

Slide 16

Slide 16 text

Bad Code type Cat struct { Type int // 0: Cat, 1:OctCat } func (c Cat) Legs() int { if c.Type == 0 { return 4 } else { return 5 } } func (c Cat) PrintLegs() { fmt.Printf("I have %d legs\n", c.Legs()) } func main() { cat := Cat{Type: 0} cat.PrintLegs() octCat := Cat{Type: 1} octCat.PrintLegs() }

Slide 17

Slide 17 text

Why bad? ࠓޙɺOctCatͷΑ͏ʹػೳ௥Ճ͕͋Δ౓ʹ Legs()ʹif-elseͷίʔυमਖ਼͕Ճ͑ΒΕΔɻ 㱺OCPʹҧ൓͍ͯ͠Δ

Slide 18

Slide 18 text

Good Code - ຒࠐύλʔϯ

Slide 19

Slide 19 text

e type Cat struct{ Name string } func (c Cat) Legs() int { return 4 } func (c Cat) PrintLegs() { fmt.Printf("I have %d legs\n", c.Legs()) } type OctoCat struct{ Cat } func (o OctoCat) Legs() int { return 5 } func (o OctoCat) PrintLegs() { fmt.Printf("I have %d legs\n", o.Legs()) } func main() { var octo OctoCat var cat Cat fmt.Println(cat.Legs()) // “4” cat.PrintLegs() // “I have 4 legs” fmt.Println(octo.Legs()) // “5” octo.PrintLegs() // “I have 5 legs” }

Slide 20

Slide 20 text

Good Code - ετϥςδʔσβΠϯύλʔϯ

Slide 21

Slide 21 text

type FelidaeInterface interface { Legs() int PrintLegs() } type Felidae struct { Name string FelidaeInterface } type Cat struct{} func (c Cat) Legs() int { return 4 } func (c Cat) PrintLegs() { fmt.Printf("I have %d legs\n", c.Legs()) } type OctoCat struct{} func (o OctoCat) Legs() int { return 5 } func (o OctoCat) PrintLegs() { fmt.Printf("I have %d legs\n", o.Legs()) } func main() { cat := Felidae{FelidaeInterface: Cat{}} octo:= Felidae{FelidaeInterface: OctoCat{}} fmt.Println(cat.Legs()) cat.PrintLegs() fmt.Println(octo.Legs()) octo.PrintLegs() }

Slide 22

Slide 22 text

Ϧείϑஔ׵ݪଇ ೿ੜΫϥεΛҰ੾ͷมߋແ͠ʹجఈΫϥεʹஔ ͖׵͑ՄೳͰ͋Δ͜ͱΛ໨ࢦ͢ɻ 㱺 ೿ੜΫϥε͸جఈΫϥεͷڍಈΛมߋ͠ͳ͍

Slide 23

Slide 23 text

In Go ͦ΋ͦ΋”ܧঝ”ͱ͍͏֓೦͕ແ͍ɻ ͦͷ୅Γʹ”ίϯϙδγϣϯ”͕͋Δ͕ɺຒΊࠐ Μͩߏ଄ମΛ׬શʹஔ͖׵͑Δ͜ͱ͕ग़དྷͳ͍ ͨΊɺLSPҧ൓ͱͳΔɻ

Slide 24

Slide 24 text

type A struct { } func (a A) Test() { fmt.Println("Printing A") } type B struct { A } func ImpossibleLiskovSubstitution(a A) { a.Test() } func main() { a := B{} ImpossibleLiskovSubstitution(a) // Aͱͯ͠ৼΔ෣͑ͣɺΤϥʔͱͳΔͨΊLSPҧ൓͍ͯ͠Δ } Bad Code

Slide 25

Slide 25 text

type A struct { } type tester interface { Test() } func (a A) Test() { fmt.Println("Printing A") } type B struct { A } func PossibleLiskovSubstitution(a tester) { a.Test() } func main() { a := B{} PossibleLiskovSubstitution(a) } Good Code

Slide 26

Slide 26 text

ຒΊࠐΜͩߏ଄ମ(A)ͷৼΔ෣͍ʹରԠ͢Δ interface(tester)Λઃఆ͠ɺinterfaceΛҾ਺ͷܕ ͱ͢Δ͜ͱͰ೿ੜͰ͋ΔຒΊࠐ·Εͨߏ଄ମ(B) Λஔ͖׵͑Մೳͱ͠ɺLSPҧ൓Λճආ͍ͯ͠Δɻ

Slide 27

Slide 27 text

ґଘੑٯసͷݪଇ ্ҐϞδϡʔϧ͸ԼҐϞδϡʔϧʹґଘͤͣɺ্ Ґ΋ԼҐ΋ந৅ʹґଘ͢΂͖Ͱ͋Δɻ 㱺 ϞδϡʔϧؒΛૄ݁߹ʹ͢Δ

Slide 28

Slide 28 text

Dave Cheneyᐌ͘

Slide 29

Slide 29 text

ʮpackageΛద੾ʹ෼͚ɺ interfaceΛ༻͍ͯந৅Λදݱ͢ Ε͹େৎ෉ɻ͋ͱɺpackageߏ ଄͸ͳΔ͚ͨฏୱʹ͠Α͏Ͷɻʯ

Slide 30

Slide 30 text

͕࣌ؒ଍Γͣ͜͜·Ͱɾɾɾ

Slide 31

Slide 31 text

·ͱΊ Dave CheneyࢯͷʮSOLID Go DesignʯΛಡ Έ·͠ΐ͏ʂʂʂʂʂ

Slide 32

Slide 32 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ