$30 off During Our Annual Pro Sale. View Details »

Designing libraries in Go way

ktr
March 10, 2021

Designing libraries in Go way

ktr

March 10, 2021
Tweet

More Decks by ktr

Other Decks in Programming

Transcript

  1. at Gopherಓ৔ ಉ૭ձ2021 Go Β͍͠ϥΠϒϥϦΛ
 ͭ͘ΔͨΊʹߟ͑Δ͜ͱ

  2. LUS !LUS@!LUS  .FSQBZ$PEFQBZNFOU5FBN #BDLFOE XIPBNJ

  3. at Gopherಓ৔ ಉ૭ձ2021 Go Β͍͠ϥΠϒϥϦΛ
 ͭ͘ΔͨΊʹߟ͑Δ͜ͱ

  4. w Ͱ΋Ͳ͏͢Ε͹ʜʁ w (PΒ͍͠ॻ͖ํɺϦϙδτϦͷઃఆɺ഑෍ͷ࢓ํʜ w ϦϙδτϦʹΑͬͯ͞·͟· (P੡ϥΠϒϥϦΛެ։͍ͨ͠ʂ

  5. (PQIFS͔Βݟͯɺ
 ͲΜͳϥΠϒϥϦͩͱخ͍͠ͷ͔ΛΫϦΞʹ͢Δ ͜ͷτʔΫͰ఻͍͑ͨ͜ͱ

  6. (PQIFS͔Βݟͯɺ
 ͲΜͳ044ͩͱخ͍͠ͷ͔ΛΫϦΞʹ͢Δ ͜ͷτʔΫͰ఻͍͑ͨ͜ͱ ࣮ࡍʹ044Λग़͢ࡍͷ໎͍Λখ͘͢͞Δʂ

  7. w $-*πʔϧݻ༗ͷϓϥΫςΟε w ಛఆͷϥΠϒϥϦ΍ϑϨʔϜϫʔΫͷ۷ΓԼ͛ ͜ͷτʔΫͰѻΘͳ͍͜ͱ

  8.  ϥΠϒϥϦઃܭͰؾΛ͚͍ͭͯΔ͜ͱ  044ͱͯ͠ެ։͢ΔͨΊʹ  ·ͱΊ ໨࣍

  9. ϥΠϒϥϦઃܭͰؾΛ͚͍ͭͯΔ͜ͱ

  10. Τϥʔ

  11. w αʔυύʔςΟͷΤϥʔϥΠϒϥϦ΍YFSSPST͸ ͔ͭΘͳ͍ w ϥΠϒϥϦͷϢʔβʹͦͷΤϥʔϥΠϒϥϦͷ
 ࢖༻Λڧ੍ͤͯ͞͠·͏ͨΊ Ͱ͖ΔݶΓඪ४ΤϥʔͷΈΛ͔ͭ͏

  12. w Ͱ͖ΔݶΓerrorsύοέʔδͷ"1*ͱޓ׵ੑ Λ࣋ͭ΋ͷΛબͿ w errors.Asɺerrors.Isɺerrors.Unwrap ඪ४ΤϥʔҎ֎Λ͔ͭ͏৔߹

  13. $POUFYU

  14. w DPOUFYUΩϟϯηϧػߏΛந৅Խͨ͠΋ͷ w ࣮ߦʹ௕͍͕͔͔࣌ؒΔՄೳੑͷ͋Δ΋ͷ͸ DPOUFYUΛαϙʔτ͢΂͖ $POUFYUBXBSF

  15. ྫLUSBQJHFO

  16. w (P੡ϥΠϒϥϦ w DVSMίϚϯυΛ࣮ߦͨ݁͠Ռ͔Β(P"1*ΫϥΠΞϯτ Λࣗಈੜ੒͢Δ LUSBQJHFO

  17. $ curl https://api.github.com/repos/ktr0731/apigen { "id": 312020626, "node_id": "MDEwOlJlcG9zaXRvcnkzMTIwMjA2MjY=", "name": "apigen",

    "full_name": "ktr0731/apigen", "private": false, "owner": { "login": "ktr0731", "id": 12953836, "node_id": "MDQ6VXNlcjEyOTUzODM2", …
  18. None
  19. None
  20. w ಺෦Ͱ"1*ϦΫΤετΛߦ͍ͬͯΔͨΊ w ͙͢ʹ݁Ռ͕ฦͬͯ͘Δͱ͸ݶΒͳ͍ w DPOUFYUΛड͚औΔ͜ͱͰ্ҐϨΠϠʔʹΩϟϯηϧɾ λΠϜΞ΢τͷ࢓૊ΈΛఏڙͰ͖Δ ͳͥDPOUFYUBXBSFͳؔ਺ͳͷ͔ʁ

  21. Φϓγϣϯ

  22. w ߏ଄ମ w GVODUJPOBMPQUJPOQBUUFSO Φϓγϣϯ

  23. w $PNNBOEܕͷϑΟʔϧυͰΦϓγϣϯ஋Λड͚Δ w TQGDPCSB

  24. w ؔ਺ͱՄม௕Ҿ਺ͷ૊Έ߹ΘͤͰΦϓγϣϯΛදݱ w ΦϓγϣϯܕΛΤΫεϙʔτ͢Δඞཁ͕ͳ͍ w ؆ܿͰ໌շͳهड़ʹͳΔ GVODUJPOBMPQUJPOQBUUFSO

  25. LUSBQJHFO

  26. LUSBQJHFO

  27. LUSBQJHFO

  28. w Ұ௕Ұ୹ w Φϓγϣϯ͕ଟ͍৔߹ɺߏ଄ମͷ΄͏͕ݟ௨͕͠ྑ͍ w (PGNU͕ϑΟʔϧυΛଗ͑ͯ͘ΕΔ w ͦΕҎ֎͸GVODUJPOBMPQUJPOQBUUFSO͕ྑͦ͞͏ ߏ଄ମWTGVODUJPOBMPQUJPOQBUUFSO

  29. w Φϓγϣϯ΋࢓༷ͷҰ෦ w ૿΍ͨ͢ͼʹϥΠϒϥϦͷෳࡶੑ͕૿͢ w ඞཁ࠷௿ݶʹอͭ ΦϓγϣϯΛ૿΍͗͢͠ͳ͍

  30. ྫ"1*ΫϥΠΞϯτͷϦτϥΠ

  31. None
  32. ϦτϥΠʹؔ͢ΔΦϓγϣϯ͕
 ͨ͘͞Μ͋ͬͯෳࡶʜ🤔

  33. Φϓγϣϯ͕Ұ͚ͭͩʹͳͬͯ
 Θ͔Γ΍͘͢ͳͬͨ☺

  34. DFOLBMUJCBDLP⒎ʹґଘͯ͠͠·͍ͬͯΔ😨

  35. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ
 ࠶౓ϦΫΤετΛ౤͛Δ ϦτϥΠʹ͍ͭͯ΋͏Ұ૚ਂ͘ߟ͑Δ

  36. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ
 ࠶౓ϦΫΤετΛ౤͛Δ w ϦτϥΠ͸)551ϦΫΤετͷͨΊʹ͔͠࢖ΘΕͳ͍ ϦτϥΠʹ͍ͭͯ΋͏Ұ૚ਂ͘ߟ͑Δ

  37. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ
 ࠶౓ϦΫΤετΛ౤͛Δ w ϦτϥΠ͸)551ϦΫΤετͷͨΊʹ͔͠࢖ΘΕͳ͍ w )551ΫϥΠΞϯτͰ࣮૷Ͱ͖ΔͷͰ͸ʜʁ ϦτϥΠʹ͍ͭͯ΋͏Ұ૚ਂ͘ߟ͑Δ

  38. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ
 ࠶౓ϦΫΤετΛ౤͛Δ w ϦτϥΠ͸)551ϦΫΤετͷͨΊʹ͔͠࢖ΘΕͳ͍ w )551ΫϥΠΞϯτͰ࣮૷Ͱ͖ΔͷͰ͸ʜʁ w ͦ΋ͦ΋ϦτϥΠʹؔͯ͜͠ͷϥΠϒϥϦͰߟ͑Δඞཁ͋Δʜʁ ϦτϥΠʹ͍ͭͯ΋͏Ұ૚ਂ͘ߟ͑Δ

  39. w ґଘ͕ඪ४ύοέʔδ͚ͩʹͳΔ w ϦτϥΠʹ·ͭΘΔؔ৺ࣄΛ෼཭Ͱ͖Δ w Ϣʔβ͸೚ҙͷϦτϥΠ࣮૷Λબ΂Δ w τϨʔε΍ϩΨʔͳͲΛ࢓ࠐΈ͍ͨ৔߹Ͱ΋ϥΠϒϥϦͷ
 Φϓγϣϯ͕૿͑ͳ͍ʂ )551ΫϥΠΞϯτΛड͚औΔϝϦοτ

  40. )551ΫϥΠΞϯτΛड͚औΔΑ͏ʹͯ͠ΈΔ

  41. None
  42. Ϣʔβ͸IBTIJDPSQHPSFUSZBCMFIUUQͰϦτϥΠΛߦ͏

  43. None
  44. (PΒ͍͠ΠϯλʔϑΣʔεΛ
 ఏڙ͢Δ

  45. w ඪ४ɾ४ඪ४ύοέʔδ͕͓खຊ w ޙํޓ׵ੑͷͨΊʹ࿪ʹͳ͍ͬͯΔՕॴ΋
 ΋ͪΖΜ͋ΔͷͰ஫ҙ (PΒ͍͠ΠϯλʔϑΣʔεΛఏڙ͢Δ

  46. ྫLUSBQJHFO

  47. w DVSMίϚϯυ w ੜ੒͢ΔϑΝΠϧͷύοέʔδ໊ w ੜ੒͢Δϝιου໊ w ϝιουͷύϥϝʔλ໊ w FUD

    BQJHFOͷೖྗ
  48. ίϚϯυϥΠϯʜʁ $ apigen --package api --out api_gen.go \ "curl example.com/post"

    "GetPost" \ "curl -d "{...}" example.com/posts" "CreatePost"
  49. ίϚϯυϥΠϯʜʁ $ apigen --package api --out api_gen.go \ "curl example.com/post"

    "GetPost" \ "curl -d "{...}" example.com/posts" "CreatePost" ੜ੒ର৅"1*͕૿͑ΔͨͼʹҾ਺͕૿͑Δ🤔 ҉໧తʹʮDVSMίϚϯυʯʮϝιου໊ʯͷϖΞΛཁٻ͍ͯͯ͠௚ײతͰ͸ͳ͍ʜ
  50. ֎෦ϑΝΠϧʜʁ $ apigen --package api --out api_gen.go definition.yaml

  51. ֎෦ϑΝΠϧʜʁ $ apigen --package api --out api_gen.go definition.yaml ࣮ߦ࣌ʹ͔͠ଥ౰ੑ͕ݕূͰ͖ͳ͍ͷͰ࢖͍ͮΒ͍🤔

  52. w DVSMίϚϯυΛ࣮ߦͨ݁͠Ռ͔Β(P"1*ΫϥΠΞϯτ Λࣗಈੜ੒͢Δ BQJHFOͷ૝ఆϢʔβΛࢥ͍ग़͢

  53. w DVSMίϚϯυΛ࣮ߦͨ݁͠Ռ͔Β(P"1*ΫϥΠΞϯτ Λࣗಈੜ੒͢Δ BQJHFOͷ૝ఆϢʔβΛࢥ͍ग़͢

  54. w DVSMίϚϯυΛ࣮ߦͨ݁͠Ռ͔Β(P"1*ΫϥΠΞϯτ Λࣗಈੜ੒͢Δ w Ϣʔβ͸(PͰ։ൃΛ͍ͯ͠Δ͜ͱΛલఏʹͰ͖Δ w (PΛ͏·͘࢖ͬͨྑ͍ղܾࡦ͕͋Γͦ͏ʜ BQJHFOͷ૝ఆϢʔβΛࢥ͍ग़͢

  55. w ४ඪ४ύοέʔδͷBOBMZTJTΛར༻ w BOBMZTJT"OBMZ[FSΛड͚औ͍ͬͯΔ HPWFUͷ࣮૷

  56. w ϑΟʔϧυͰ͞·͟·ͳઃఆΛ
 ߦ͍ͬͯΔ BOBMZTJT"OBMZ[FS

  57. w ϑΟʔϧυͰ͞·͟·ͳઃఆΛ
 ߦ͍ͬͯΔ BOBMZTJT"OBMZ[FS

  58. w BOBMZTJTύοέʔδͷΑ͏ʹઃఆΛߦ͏ܕΛ
 ఆٛ͠ɺΤϯτϦϙΠϯτؔ਺΁౉͢ ྑͦ͞͏ͳղܾࡦ

  59. None
  60. "1*ఆٛΛߦ͏

  61. ίʔυੜ੒ʂ

  62. None
  63. w ੩తܕ෇͚͞ΕͨίʔυδΣωϨʔλ w "1*ఆٛΛॻ͍ͯapigen.Generate΁౉͚ͩ͢ w ΦϓγϣϯͰio.WriterΛड͚औͬͨΓ͢Δ͜ͱ΋Մೳʂ ސ٬͕ຊ౰ʹཉ͔ͬͨ͠΋ͷ

  64. ྫLUSHPGV[[ZpOEFS

  65. w (P੡ϥΠϒϥϦ w G[GͷΑ͏ͳGV[[ZpOEFSΛ(P੡$-*πʔϧ ʹ૊ΈࠐΊΔ LUSHPGV[[ZpOEFS

  66. None
  67. ֤ީิΛදࣔ͢ΔͨΊʹ͸ʜ🤔

  68. ಠࣗܕͰީิΛ౉ͯ͠΋Β͏ʁ

  69. Ϣʔβ͸ຖճ[]*CandidateΛͭ͘Βͳ͍ͱ ͍͚ͳ͍ͷͰ͋·Γ࢖͍উख͕ྑ͘ͳ͍ʜ

  70. w δΣωϦΫε͸ ·ͩ ͳ͍ ೚ҙͷܕͷεϥΠεΛѻ͏ʹ͸ʜʁ

  71. w δΣωϦΫε͸ ·ͩ ͳ͍ w ඪ४ύοέʔδʹ೚ҙͷܕͷεϥΠεΛѻ͍ͬͯ Δؔ਺͕͋ͬͨΑ͏ͳʜʁ ೚ҙͷܕͷεϥΠεΛѻ͏ʹ͸ʜʁ

  72. TPSU4MJDF

  73. None
  74. None
  75. εϥΠεͷܕʹ͸ؔ༩ͤͣɺީิJͷλΠτϧΛ ܾఆ͢ΔͨΊͷؔ਺Λड͚औΔΑ͏ʹ

  76. εϥΠεͰ͋Ε͹ͳΜͰ΋౉ͤΔΑ͏ʹʂ

  77. None
  78. 044ͱͯ͠ެ։͢ΔͨΊʹ

  79. w (JU)VC͕ެ։͍ͯ͠Δ044ͷΨΠυ w 044ͷ8)"58):ɺߩݙํ๏ɺϓϩδΣΫτͷ
 ্ཱͪ͛ํͳͲΛ໢ཏ͍ͯ͠Δ w ୯७ʹಡΈ෺ͱͯ͠΋͓΋͠Ζ͍ 0QFO4PVSDF(VJEFTΛಡΉ

  80. https://opensource.guide/ja/starting-a-project/

  81. w ϓϩδΣΫτͷґଘ؅ཧͷͨΊ w όʔδϣϯ؅ཧ͞ΕͨϞδϡʔϧͱͯ͠ఏڙ͢ΔͨΊ (PNPEVMFTʹରԠ͢Δ

  82. $ go mod init && go mod tidy (PNPEVMFTʹରԠ͢Δ

  83. $ go mod init && go mod tidy (PNPEVMFTʹରԠ͢Δ ˞λά෇͚ʹ͍ͭͯ͸ޙड़

  84. w ࣗ෼ͷ044͕ґଘ͍ͯ͠ΔϞδϡʔϧͷ
 ϥΠηϯε΋දه͢΂͖ w 4POHNVHPDSFEJUT͕΂ΜΓ ґଘϞδϡʔϧͷϥΠηϯεදه

  85. $ gocredits -w . ґଘϞδϡʔϧͷϥΠηϯεදه

  86. w (PNPEVMFTͰMJOUFSͳͲͷπʔϧ܈΋؅ཧ͢Δ w Ϗϧυ΍ςετͷ࠶ݱੑΛ΋ͨͤΔͨΊ (Pπʔϧͷґଘ؅ཧ

  87. https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module

  88. w (JUλάΛ੾ͬͯQVTI͢Ε͹৽͍͠Ϟδϡʔϧ ͱͯ͠ೝࣝ͞ΕΔ ϥΠϒϥϦͷ഑෍

  89. w (P3FMFBTFSΛ͔ͭ͏ͱΑΓྑ͍͔΋ w (JU)VC3FMFBTFTͷ࡞੒ɺ$IBOHFMPHͷ࡞੒Λ Α͠ͳʹ΍ͬͯ͘ΕΔ ϥΠϒϥϦͷ഑෍

  90. $ cat .goreleaser.yaml builds: - skip: true $ git tag

    v0.1.0 main $ goreleaser ϥΠϒϥϦͷ഑෍
  91. w 044ͱͯ͠ग़͢ϥΠϒϥϦ͸ϢʔβͷͨΊʹ
 υΩϡϝϯτΛॻ͘΂͖ υΩϡϝϯτ

  92. w Ϣʔβͷࢹ఺ͰυΩϡϝϯτΛಡΊΔ w (P%PD͚ͩͰ࢖͍ํ͕ཧղͰ͖Δ͔ʁ w ެ։͢Δඞཁͷͳ͍΋ͷͳ͍͔ʁ w ࢓༷ͷ࿙Ε͸ͳ͍͔ʁ (P%PDΛ࢖ͬͨυΩϡϝϯτͷચ࿅

  93. w ਓʑ͸υΩϡϝϯτΛಡ·ͳ͍ w &YBNQMFςετ͕͋Ε͹ɺͦͷϥΠϒϥϦͷ࣭ ͕͋Δఔ౓Θ͔Δ &YBNQMFςετΛ༻ҙ͢Δ

  94. None
  95. w &YBNQMFςετΑΓෳࡶͳ࢖༻ྫΛஔ͍͓ͯ͘ @FYBNQMFσΟϨΫτϦ

  96. w @΍͔Β࢝·ΔϑΝΠϧɾσΟϨΫτϦ
 ͓ΑͼUFTUEBUBͱ͍͏σΟϨΫτϦ͸
 (Pπʔϧ͔Βແࢹ͞ΕΔ w @FYBNQMFͱ͍͏໋໊͸׳श ಛघͳϑΝΠϧ໊ɾσΟϨΫτϦ໊ https://golang.org/cmd/go/#hdr-Package_lists_and_patterns

  97. ·ͱΊ

  98. w αʔυύʔςΟ΁ͷґଘΛগͳ͘ɾখ͘͞อͭ w ࢓༷Λগͳ͘ɾখ͘͞อͭ w DPOUFYUΛͪΌΜͱαϙʔτ͢Δ w (PΒ͍͠ΠϯλʔϑΣʔεΛఏڙ͢Δ w ඪ४ɾ४ඪ४ύοέʔδ͕ͱͯ΋ࢀߟʹͳΔ

    ϥΠϒϥϦઃܭͰؾΛ͚͍ͭͯΔ͜ͱ
  99. w 0QFO4PVSDF(VJEFTΛಡΜͰ͓͘ͱྑ͍͔΋ w (PNPEVMFTͰϥΠϒϥϦͷόʔδϣϯ؅ཧΛ͠Α͏ w υΩϡϝϯτ͸͕Μ͹ͬͯॻ͜͏ w ࣗ෼͸ϒϥογϡΞοϓલʹॻ͘͜ͱ͕ଟ͍ 044ͱͯ͠ެ։͢ΔͨΊʹ