Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go in action 輪読会 Chapter5 5.4-5.6

Makoto Tano
January 15, 2016

Go in action 輪読会 Chapter5 5.4-5.6

- 5.4 Interface (インターフェース型)
- 5.5 type embedding(埋め込み)
- 5.6 Exporting and unexporting identifiers(外部パッケージからの参照)

Makoto Tano

January 15, 2016
Tweet

More Decks by Makoto Tano

Other Decks in Programming

Transcript

  1. GO IN ACTION ྠಡձ 1/14 Go In Action Chapter 5

    Go’s type system - 5.4 Interface (ΠϯλʔϑΣʔεܕʣ - 5.5 type embeddingʢຒΊࠐΈʣ - 5.6 Exporting and unexporting identifiersʢ֎෦ύοέʔδ͔Βͷࢀরʣ by Makoto Tano @
  2. GO IN ACTION ྠಡձ 1/14 • ”ৼΔ෣͍”Λܕ΁ू໿͢Δ͜ͱͰGoͰ͸ϙϦϞʔϑΟζϜΛ Մೳͱ͍ͯ͠·͢ɻ • Interfaceܕͱ͸”ৼΔ෣͍”͚ͩΛఆٛͨ͠΋ͷͰ͢ɻ

    • “ৼΔ෣͍" ͷ಺༰͸Interfaceܕ΁௚઀ఆٛ͠·ͤΜɻ • Interfaceʹఆٛͨ͠method setΛ΋ͭϢʔβఆٛܕͷΈ Λɺ͜ͷInterfaceܕʹ୅ೖͰ͖·͢ɻ • ͜ͷΑ͏ʹInterfaceͷ༻݅Λຬͨͨ͠ϢʔβʔఆٛܕΛ concrete type ͱݺͼ·͢ɻ • https://play.golang.org/p/_eneJprgla
  3. GO IN ACTION ྠಡձ 1/14 • interface஋͸̎ͭͷσʔλߏ଄͔Β੒Γཱ͍ͬͯ·͢ • ͻͱͭΊ͸ɺ iTableͱݺ͹ΕΔ಺෦ςʔϒϧ΁ͷϙΠϯλ

    • ;ͨͭΊ͸ɺ֨ೲ͍ͯ͠Δ஋΁ͷϙΠϯλʢ̎ʣ • iTable΋̎ͭͷσʔλߏ଄͔Β੒Γཱ͍ͬͯ·͢ • ֨ೲ͍ͯ͠Δ஋ͷܕ৘ใʢ̍ʣ • ஋ʹଐ͍ͯ͠ΔϝιουͷϦετ • ͜ͷܕ৘ใʢ̍ʣͱϙΠϯλʔʢ̎ʣ͕Interfaceͱ஋Λbind͍ͯ͠·͢ɻ Address myDog Address iTable notifier
 Interface value MyDog Stored value Method set Type(myDog) iTable var d Dog d = MyDog{“Pochi”}
  4. GO IN ACTION ྠಡձ 1/14 • interface ʹ஋΍ϙΠϯλʔΛ୅ೖͨ࣌͠ɺiTableͷ৘ใ͸Ͳ ͏ͳΓ·͔͢ʁ Address

    myDog Address iTable notifier
 Interface value MyDog Stored value Method set Type(myDog) iTable var d Dog d = MyDog{“Pochi”} Address myDog Address iTable notifier
 Interface value MyDog Stored value Method set Type(*myDog) iTable var d Dog d = &MyDog{“Pochi”} • value assignment • pointer assignment • iTableͷܕ͸
 Reflectionͨ͠ܕΛ
 ࣋ͭ
  5. GO IN ACTION ྠಡձ 1/14 • Method ͷ value ϨγʔόʔͱɺpointerϨγʔόʔͷϧʔϧ

    • https://play.golang.org/p/Uopo2StNfA • Method set͸ɺ༩͑ΒΕͨܕͷ஋΋͘͠͸ϙΠϯλʔͷͲͪΒ ͔ʹఆٛΛ͢Δ΋ͷ͢Δɻ • Ϩγʔόʔͷܕ͸ɺ༩͑ΒΕͨMethod͕஋ɺϙΠϯλʔɺ΋͠ ͘͸ͦͷ྆ํʹଐ͢Δ΂͖ͳͷ͔Λܾఆ͠·͢ɻ • ҎԼͷ༷ͳϧʔϧͰ͢ɻ • ઌ΄ͲͷMyCat͸PointerͰ౉ͤ͹Α͔ͬͨͱ͍͏͜ͱͰ͢
 https://play.golang.org/p/3ejDDyP37G ஋ MethodϨγʔόʔ T (t T) *T (t T) and (t *T)
  6. GO IN ACTION ྠಡձ 1/14 • Method ͷ value ϨγʔόʔͱɺpointerϨγʔόʔͷϧʔϧ

    • ٯʹϨγʔόʔଆ͔ΒݟΔͱҎԼͷ༷ͳϧʔϧͰ͢ɻ MethodϨγʔόʔ ஋ (t T) T and *T (t *T) *T
  7. GO IN ACTION ྠಡձ 1/14 • https://play.golang.org/p/d8IQIT2zSL • ͜Μͳ࿩΋ཧղͰ͖·͢Ͷɻ •

    ϙΠϯλϨγʔόʔ͕࣮ߦͰ͖Δ৚݅ͱ͸ • Method set͕ϙΠϯλʹଐ͍ͯ͠ΔͷͰɺϙΠϯλͱͯ͠ ΞυϨε͕औΕ͍ͯͳ͍ม਺͔Β͸ɺMethod set͕࣮ߦͰ ͖ͳ͍
 ͱ͍͏͜ͱͰ͢ɻ
  8. GO IN ACTION ྠಡձ 1/14 • InterfaceΛ༻͍ͨϙϦϞʔϑΟζϜ • https://play.golang.org/p/h0leVMuJd9 •

    Interface͕”ৼΔ෣͍”ͱ”ॲཧ”Λ෼͚͍ͯΔͷ Ͱɺಉ͡InterfaceʹҟͳΔॲཧΛ͢ΔΦϒδΣ ΫτΛ౉͍ͯ͠Δͱɺಉ͡खଓ͖ͰҟͳΔॲཧ Λ࣮ݱͰ͖·͢ʢϙϦϞʔϑΟζϜʣ • αϥοͱͰ͍͢·ͤΜɻʢ࿩͢ΑΓίʔυͷݟ ͨ΄͏͕ૣ͍Ͱ͢ΑͶɾɾʁʣ
  9. GO IN ACTION ྠಡձ 1/14 • https://github.com/goinaction/code/blob/master/chapter5/listing50/ listing50.go • ϝοηʔδ

    • ߏ଄ମ in ߏ଄ମ ʢຒΊࠐΈʣ͕ՄೳͰ͢ • ad.user.nortify()ͱ͍͏ܗͰຒΊࠐΜͩߏ଄ମͷMethodΛίʔϧ Ͱ͖·͢ɻ • ad.nortify() ͱ͍͏ײ͡Ͱɺ਌ͷadminߏ଄ମ΁Method͕͔͋ͨ ΋͋Δ͔ͷΑ͏ʹʢPromoteͨ͠ʣίʔϧ͢Δ͜ͱ͕Ͱ͖·͢ɻ • ʢΫϥεͰ͍͏ͱجఈΫϥεͷܧঝΈ͍ͨͳΠϝʔδͰ͢Ͷɻҙ ຯશ͘ҧ͍·͕͢ɾɾɾɻʣ
  10. GO IN ACTION ྠಡձ 1/14 • ߏ଄ମA -> ߏ଄ମ B

    ຒΊࠐΈ·͢ • Interface `nortifier` ͕ nortify() ͱ͍͏ MethodΛ͍࣋ͬͯͯɺߏ଄ ମA΋ɺߏ଄ମB΋ nortify()ͱ͍͏ؔ਺Λ͍࣋ͬͯΔʹ concrete type ͩͱ͠·͢ɻ • func hoge(n nortifier) ʹɺߏ଄ମAΛ౉ͯ͠ɺn.nortifyʢʣΛ࣮ߦ ͢ΔͱͲ͏ͳΔͰ͠ΐ͏͔ʁ • ౴͑ɿ ߏ଄ମAͷ nortify() ͕ίʔϧ͞Ε·͢ɻ
 ʢߏ଄ମBͷnortify() ͸Promote͠·ͤΜʣ • https://play.golang.org/p/Y9eqwuspb6
  11. GO IN ACTION ྠಡձ 1/14 • GoͰ͸ม਺ͷઌ಄จࣈ͕େจࣈ͔খจࣈ͔Ͱɺଞͷύοέʔδ͔ΒࢀরͰ͖ΔʗͰ͖ͳ͍ ͕ܾ·Γ·͢ɻ • ύοέʔδ͸ͦͷίʔυ͕ೖ͍ͬͯΔϑΥϧμ໊ͱಉҰʹ͢Δͷ͕Good

    PracticeͰ͢ɻ • Ҏ্Ͱ͢ɻʢ࠷ޙଉ੾ΕΛͨ͠ͷͰͳ͘ɺຊ౰ʹίϨҎ্ͷ಺༰͕ͳ͔ͬͨͷͰ͢ɻɻʣ • αϯϓϧ • ؔ਺ͷ֎෦ύοέʔδͷࢀর • https://github.com/goinaction/code/tree/master/chapter5/listing64 • func New() Ͱճආ͢Δ
 https://github.com/goinaction/code/tree/master/chapter5/listing68 • ߏ଄ମϑΟʔϧυͷ֎෦ύοέʔδͷࢀর • https://github.com/goinaction/code/tree/master/chapter5/listing71 • ຒΊࠐΜͩߏ଄ମϑΟʔϧυpromote ࢀর
 https://github.com/goinaction/code/tree/master/chapter5/listing74