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

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 Β͍͠ϥΠϒϥϦΛ

    ͭ͘ΔͨΊʹߟ͑Δ͜ͱ

    View full-size slide

  2. LUS !LUS@!LUS

    .FSQBZ$PEFQBZNFOU5FBN #BDLFOE

    XIPBNJ

    View full-size slide

  3. at Gopherಓ৔ ಉ૭ձ2021
    Go Β͍͠ϥΠϒϥϦΛ

    ͭ͘ΔͨΊʹߟ͑Δ͜ͱ

    View full-size slide

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

    View full-size slide

  5. (PQIFS͔Βݟͯɺ

    ͲΜͳϥΠϒϥϦͩͱخ͍͠ͷ͔ΛΫϦΞʹ͢Δ
    ͜ͷτʔΫͰ఻͍͑ͨ͜ͱ

    View full-size slide

  6. (PQIFS͔Βݟͯɺ

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. w αʔυύʔςΟͷΤϥʔϥΠϒϥϦ΍YFSSPST͸
    ͔ͭΘͳ͍
    w ϥΠϒϥϦͷϢʔβʹͦͷΤϥʔϥΠϒϥϦͷ

    ࢖༻Λڧ੍ͤͯ͞͠·͏ͨΊ
    Ͱ͖ΔݶΓඪ४ΤϥʔͷΈΛ͔ͭ͏

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. ྫLUSBQJHFO

    View full-size slide

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

    View full-size slide

  15. $ 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",

    View full-size slide

  16. w ಺෦Ͱ"1*ϦΫΤετΛߦ͍ͬͯΔͨΊ
    w ͙͢ʹ݁Ռ͕ฦͬͯ͘Δͱ͸ݶΒͳ͍
    w DPOUFYUΛड͚औΔ͜ͱͰ্ҐϨΠϠʔʹΩϟϯηϧɾ
    λΠϜΞ΢τͷ࢓૊ΈΛఏڙͰ͖Δ
    ͳͥDPOUFYUBXBSFͳؔ਺ͳͷ͔ʁ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. ϦτϥΠʹؔ͢ΔΦϓγϣϯ͕

    ͨ͘͞Μ͋ͬͯෳࡶʜ🤔

    View full-size slide

  24. Φϓγϣϯ͕Ұ͚ͭͩʹͳͬͯ

    Θ͔Γ΍͘͢ͳͬͨ☺

    View full-size slide

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

    View full-size slide

  26. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ

    ࠶౓ϦΫΤετΛ౤͛Δ
    ϦτϥΠʹ͍ͭͯ΋͏Ұ૚ਂ͘ߟ͑Δ

    View full-size slide

  27. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ

    ࠶౓ϦΫΤετΛ౤͛Δ
    w ϦτϥΠ͸)551ϦΫΤετͷͨΊʹ͔͠࢖ΘΕͳ͍
    ϦτϥΠʹ͍ͭͯ΋͏Ұ૚ਂ͘ߟ͑Δ

    View full-size slide

  28. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ

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

    View full-size slide

  29. w )551ϦΫΤετʹࣦഊͨ͠ͱ͖ʹҰఆ࣌ؒ଴ͪɺ

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

    View full-size slide

  30. w ґଘ͕ඪ४ύοέʔδ͚ͩʹͳΔ
    w ϦτϥΠʹ·ͭΘΔؔ৺ࣄΛ෼཭Ͱ͖Δ
    w Ϣʔβ͸೚ҙͷϦτϥΠ࣮૷Λબ΂Δ
    w τϨʔε΍ϩΨʔͳͲΛ࢓ࠐΈ͍ͨ৔߹Ͱ΋ϥΠϒϥϦͷ

    Φϓγϣϯ͕૿͑ͳ͍ʂ
    )551ΫϥΠΞϯτΛड͚औΔϝϦοτ

    View full-size slide

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

    View full-size slide

  32. Ϣʔβ͸IBTIJDPSQHPSFUSZBCMFIUUQͰϦτϥΠΛߦ͏

    View full-size slide

  33. (PΒ͍͠ΠϯλʔϑΣʔεΛ

    ఏڙ͢Δ

    View full-size slide

  34. w ඪ४ɾ४ඪ४ύοέʔδ͕͓खຊ
    w ޙํޓ׵ੑͷͨΊʹ࿪ʹͳ͍ͬͯΔՕॴ΋

    ΋ͪΖΜ͋ΔͷͰ஫ҙ
    (PΒ͍͠ΠϯλʔϑΣʔεΛఏڙ͢Δ

    View full-size slide

  35. ྫLUSBQJHFO

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  39. ֎෦ϑΝΠϧʜʁ
    $ apigen --package api --out api_gen.go definition.yaml

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  45. w ϑΟʔϧυͰ͞·͟·ͳઃఆΛ

    ߦ͍ͬͯΔ
    BOBMZTJT"OBMZ[FS

    View full-size slide

  46. w ϑΟʔϧυͰ͞·͟·ͳઃఆΛ

    ߦ͍ͬͯΔ
    BOBMZTJT"OBMZ[FS

    View full-size slide

  47. w BOBMZTJTύοέʔδͷΑ͏ʹઃఆΛߦ͏ܕΛ

    ఆٛ͠ɺΤϯτϦϙΠϯτؔ਺΁౉͢
    ྑͦ͞͏ͳղܾࡦ

    View full-size slide

  48. "1*ఆٛΛߦ͏

    View full-size slide

  49. ίʔυੜ੒ʂ

    View full-size slide

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

    View full-size slide

  51. ྫLUSHPGV[[ZpOEFS

    View full-size slide

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

    View full-size slide

  53. ֤ީิΛදࣔ͢ΔͨΊʹ͸ʜ🤔

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  60. 044ͱͯ͠ެ։͢ΔͨΊʹ

    View full-size slide

  61. w (JU)VC͕ެ։͍ͯ͠Δ044ͷΨΠυ
    w 044ͷ8)"58):ɺߩݙํ๏ɺϓϩδΣΫτͷ

    ্ཱͪ͛ํͳͲΛ໢ཏ͍ͯ͠Δ
    w ୯७ʹಡΈ෺ͱͯ͠΋͓΋͠Ζ͍
    0QFO4PVSDF(VJEFTΛಡΉ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  66. w ࣗ෼ͷ044͕ґଘ͍ͯ͠ΔϞδϡʔϧͷ

    ϥΠηϯε΋දه͢΂͖
    w 4POHNVHPDSFEJUT͕΂ΜΓ
    ґଘϞδϡʔϧͷϥΠηϯεදه

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  72. $ cat .goreleaser.yaml
    builds:
    - skip: true
    $ git tag v0.1.0 main
    $ goreleaser
    ϥΠϒϥϦͷ഑෍

    View full-size slide

  73. w 044ͱͯ͠ग़͢ϥΠϒϥϦ͸ϢʔβͷͨΊʹ

    υΩϡϝϯτΛॻ͘΂͖
    υΩϡϝϯτ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  77. w @΍͔Β࢝·ΔϑΝΠϧɾσΟϨΫτϦ

    ͓ΑͼUFTUEBUBͱ͍͏σΟϨΫτϦ͸

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide