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

GolangCI を使ってコードの品質を保ちながら快適な Golang 生活を送る話 #gounco / GolangCI

d-kuro
January 31, 2019

GolangCI を使ってコードの品質を保ちながら快適な Golang 生活を送る話 #gounco / GolangCI

Go(Un)Conference(Goあんこ)LT大会 5kg で発表した資料です
https://gounconference.connpass.com/event/112942/

d-kuro

January 31, 2019
Tweet

More Decks by d-kuro

Other Decks in Programming

Transcript

  1. GolangCI Λ࢖ͬͯ
    ίʔυͷ඼࣭Λอͪͳ͕Β
    շదͳ Golang ੜ׆ΛૹΔ࿩
    19/01/31 Go(Un)ConferenceʢGo͋Μ͜ʣLTେձ 5kg
    Dai Kurosawa / @ponde_m

    View full-size slide

  2. Έͳ͞ΜɺLint ࢖ͬͯ·͔͢

    View full-size slide

  3. ઌ೔ͷ golang.tokyo #21
    Ͱ΋͜Μͳ͓࿩͕͋Γ·ͨ͠

    View full-size slide

  4. ՝୊ͷϑΟʔυόοΫ
    From: 34ਓͷGoͷ՝୊ΛϨϏϡʔͯ͠ײͨ͜͡ͱ

    (https://docs.google.com/presentation/d/1TAwxT9mRmiEjQOZurz-TbXc8SQf0mlEJDS8tL49Q-M4)

    View full-size slide

  5. ՝୊ͷϑΟʔυόοΫ
    From: 34ਓͷGoͷ՝୊ΛϨϏϡʔͯ͠ײͨ͜͡ͱ

    (https://docs.google.com/presentation/d/1TAwxT9mRmiEjQOZurz-TbXc8SQf0mlEJDS8tL49Q-M4)

    View full-size slide

  6. πʔϧ࢖͍͖ͬͯ·͠ΐ͏ʂ

    View full-size slide

  7. ࠓ೔͸͍ͭ͜ͷ࿩Ͱ͢

    View full-size slide

  8. GolangCI ?
    ʕ ◔ϖ◔ʔ

    View full-size slide

  9. GolangCI ͱ͸
    • GitHub ͷ Pull Request ͷ໰୊Λݕ஌ͯ͠ίϝϯτͯ͘͠ΕΔ Golang ༻
    ͷ CI αʔϏε
    • ͍ΖΜͳ Linter Λ࣮ߦͯ͘͠ΕΔ
    • OSS ͳΒແྉͰ࢖͑Δ
    • Private Repo ͸1Ϣʔβ $20/mo
    • GolangCI ࣗମ΋ OSS ͳͷͰιʔε͕શ෦ެ։͞Ε͍ͯΔ
    • https://github.com/golangci
    Image From: https://golangci.com/

    View full-size slide

  10. ࣮ߦͯ͘͠ΕΔ Linter
    • શ 31 छྨ (19/01/31 ݱ࣌఺ ͷυΩϡϝϯτ্)
    • ΊͬͪΌ͋Δ
    • ࣮ࡍ͸ Linter ͕ merge ͞ΕͨΓͯ͠ΔͷͰਖ਼֬ͳछྨ਺
    ͱͯ͠͸΋ͬͱগͳ͔ͬͨΓ͢Δ
    • جຊͷ gofmt, goimports, govet ͔Β

    errorcheck ͳͲҰൠతʹ࢖ΘΕͯͦ͏ͳ΋ͷ͸໢ཏ͞Ε
    ͯΔ͸ͣ

    View full-size slide

  11. ࣮ߦΠϝʔδ

    View full-size slide

  12. ࢖͍ํ
    • https://golangci.com/ ʹߦͬͯ
    GitHub ೝূͯ͠ϦϙδτϦબ୒
    ͢Δ͚ͩ
    • 30 ඵͰऴΘΔͷͰΈΜͳ΍Ζ͏ʂ

    View full-size slide

  13. ద༻͢Δ Linter ͷΧελϚΠζ
    • .golangci.yml ʹ৭ʑॻ͍ͯ͋͛Δ
    • σϑΥϧτͩͱແޮʹͳ͍ͬͯΔ
    Linter ͕݁ߏଟ͍ͷͰ஫ҙ
    • https://github.com/golangci/golangci-
    lint#enabled-by-default-linters

    View full-size slide

  14. Ͱ΋ Linter ͸ CI ͚ͩͰ͸ͳ͘
    ϩʔΧϧͰ΋ಈ͔͍ͨ͠
    (push ͠ͳ͍ͱ݁ՌݟΕͳ͍ͷ͸ͩΔ͍)

    View full-size slide

  15. GolangCI-Lint

    View full-size slide

  16. Golang-CI Lint
    • ϩʔΧϧͰ GolangCI Ͱಈ͘ Lint Λ࣮ߦͰ͖Δ
    • CircleCI ͳͲͷ CI αʔϏεʹ૊ΈࠐΉ
    • VSCode ͳͲͷΤσΟλͱ౷߹Ͱ͖Δ
    • ࣅͨΑ͏ͳ Linter ʹ gometalinter ͕ଘࡏ͢Δ͕ͦΕΑΓ΋ߴ଎ʹಈ࡞
    ͢Δͷ͕ಛ௃
    • ଞʹ΋৭ʑվળ͞Ε͍ͯΔ (ޙड़)
    • Istio ΍ kustomize Ͱ࢖ΘΕ͍ͯͨΓ͢Δ

    View full-size slide

  17. gometalinter ͱͷൺֱ
    From: https://github.com/golangci/golangci-lint#golangci-lint-vs-gometalinter

    View full-size slide

  18. gometalinter ͱͷൺֱ
    • 2 ~ 7ഒ͘Β͍଎͍
    • 26%͘Β͍ϝϞϦফඅ͕গͳ͍
    • ฏߦॲཧ਺ͪΌΜͱ੍ޚͰ͖Δ
    • ग़ྗϑΥʔϚοτΛΧελϚΠζͰ͖Δ
    • আ֎ϦετͳͲΛ࢖ͬͯҰൠతͳޡݕ஌ʹରԠ
    • Git ͷεςʔδϯά, τϥοΩϯά͞Εͯͳ͍ϑΝΠϧͷΈʹ࣮ߦ,

    ಛఆͷϦϏδϣϯҎ߱ͷίϛοτʹର࣮ͯ͠ߦΈ͍ͨͳઃఆ͕Ͱ͖ΔͷͰେن໛ͳίʔυ
    ϕʔεʹ΋ಋೖ͕Ͱ͖Δ
    • Linter ͷݸผͷΠϯετʔϧ͕ෆཁͰ GolangCI-Lint ͚ͩΠϯετʔϧ͢Ε͹Α͍
    • YAML Ͱઃఆ͕Ͱ͖Δ
    From: https://github.com/golangci/golangci-lint#golangci-lint-vs-gometalinter

    View full-size slide

  19. gometalinter ͱͷൺֱ
    From: https://github.com/golangci/golangci-lint#comparison-with-gometalinter

    View full-size slide

  20. ΊͬͪΌൺֱͯ͠Δ

    View full-size slide

  21. Φεεϝͷ࢖͍ํ (?)
    • ೴ࢮͰ enable-all
    • શ෦ͷ Linter ͕ಈ͘

    View full-size slide

  22. ͱݴ͍͍ͨͱ͜Ζ͚ͩͲ
    ྲྀੴʹͭΒ͍

    View full-size slide

  23. ͭΒ͍ྫ
    • gochecknoinits
    • init ؔ਺͕͋ΔͱౖΒΕΔ
    From: https://github.com/leighmcculloch/gochecknoinits

    View full-size slide

  24. Lint ͸ద੾ʹ࢖͏
    • ݫ͗͢͠Δ Lint ͸ෛ୲ʹͳΔ
    • ಛʹ CI ͷϫʔΫϑϩʔʹ૊Έࠐ·Ε͍ͯΔ৔߹
    ͸ڧ੍ྗ͕͋ΔͷͰ஫ҙ͢Δ
    • νʔϜ։ൃͷ৔߹ͳͲ͸ແཧͳ͘औΓ૊ΊΔΑ͏ɺ
    খ͘͞গͣͭ͠ಋೖͯ͘͠
    • ద੾ͳબ୒ɺΧελϚΠζΛ͠Α͏

    View full-size slide

  25. Ͳͷ Linter Λ࢖͏໰୊

    View full-size slide

  26. @ponde_m ͷීஈ
    • ΘΓͱ೴ࢮͯ͠Δ
    • ແޮʹ͍ͯ͠Δ Linter
    • gochecknoglobals
    • άϩʔόϧείʔϓͷม਺͕͋ͬͨΒౖΒΕΔ
    • gochecknoinits
    • init ؔ਺͕͋ͬͨΒౖΒΕΔ

    View full-size slide

  27. ͜ͷ··ͩͱ
    ͋·ΓࢀߟʹͳΒͳ͍ͷͰ

    View full-size slide

  28. ࢖͍ͬͯΔ Linter Λ
    ެ։͍ͯ͠Δձࣾ͞Μͷ঺հ

    View full-size slide

  29. ϝϧΧϦ͞Μ
    From: Microservices implementation guide in Go at Mercari Inc

    (https://speakerdeck.com/ygnmhdtt/microservices-implementation-guide-in-go-at-mercari-inc?slide=27)

    View full-size slide

  30. αΠϘ΢ζ͞Μ
    From: Neco ϓϩδΣΫτͷεΩϧνΣοΫγʔτ

    (https://gist.github.com/ymmt2005/bd92296166e52d1beba9df8ac516a9db)

    View full-size slide

  31. ͔͜͜Β͸
    ࠓग़͖ͯͨ Linter
    +
    ͓͢͢Ί Linter ͷ
    ঺հίʔφʔ

    View full-size slide

  32. gofmt
    • ίʔυ੔ܗ
    • جຊ

    View full-size slide

  33. gofmt -s
    • -s ΦϓγϣϯΛ͚ͭΔͱίʔυΛγϯϓϧʹͯ͘͠ΕΔ

    View full-size slide

  34. goimports
    • gofmt + import ͷमਖ਼Λͯ͘͠ΕΔ
    • جຊ

    View full-size slide

  35. golint
    • Effective Go ΍ CodeReviewComments ʹج͍ͮ
    ͯίʔσΟϯάελΠϧΛࢦఠͯ͘͠ΕΔ
    From: Effective Go

    (https://golang.org/doc/effective_go.html)

    From: CodeReviewComments

    (https://github.com/golang/go/wiki/CodeReviewComments)

    View full-size slide

  36. govet
    • ίϯύΠϥ͕ݕग़ͯ͘͠Εͳ͍ΤϥʔΛࢦఠͯ͘͠ΕΔ

    View full-size slide

  37. errcheck
    • error ͷϋϯυϦϯάͯ͠ͳ͍ͱౖΒΕΔ

    View full-size slide

  38. staticcheck
    • αʔυύʔςΟ੡͚ͩͲ৭ʑͳ໰୊Λݕ஌ͯ͘͠ΕΔ
    • govet ͬΆ͍΍ͭ
    • νΣοΫͯ͘͠ΕΔ΋ͷ͸ຊ౰ʹ৭ʑ͋ΔͷͰυΩϡϝϯτࢀর
    • https://staticcheck.io/docs/checks

    View full-size slide

  39. misspell
    • ίϝϯτ಺ͷεϖϧϛεΛݟ͚ͭͯ͘ΕΔ

    View full-size slide

  40. interfacer
    • ͜͜ͷҾ਺ΠϯλϑΣʔεʹͰ͖ΔΑΈ͍ͨͳͷΛڭ͑ͯ͘ΕΔ

    View full-size slide

  41. prealloc
    • ࣄલʹׂΓ౰ͯՄೳͳεϥΠεΛݟ͚ͭͯ͘ΕΔ

    View full-size slide

  42. lll
    • 1ߦ௕͗͢໰୊Λڭ͑ͯ͘ΕΔ

    View full-size slide

  43. ·ͱΊ
    • GolangCI-Lint ͸୯ମͰ·ͱΊͯ Linter Λ࣮ߦ
    Ͱ͖ΔΠέͯΔπʔϧ
    • Linter ͸ద੾ʹઃఆ͢Δ
    • ಋೖ͸খ͘͞গͣͭ͠
    • ݸਓɺνʔϜʹ߹ΘͤͨΧελϚΠζΛ

    View full-size slide

  44. ͓͠·͍
    ʕ ◔ϖ◔ʔ

    View full-size slide