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

Cad656ed619672b702191833dc819943?s=47 d-kuro
January 31, 2019

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

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

Cad656ed619672b702191833dc819943?s=128

d-kuro

January 31, 2019
Tweet

Transcript

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

    Kurosawa / @ponde_m
  2. Introduce

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

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

  5. ՝୊ͷϑΟʔυόοΫ From: 34ਓͷGoͷ՝୊ΛϨϏϡʔͯ͠ײͨ͜͡ͱ
 (https://docs.google.com/presentation/d/1TAwxT9mRmiEjQOZurz-TbXc8SQf0mlEJDS8tL49Q-M4)

  6. ՝୊ͷϑΟʔυόοΫ From: 34ਓͷGoͷ՝୊ΛϨϏϡʔͯ͠ײͨ͜͡ͱ
 (https://docs.google.com/presentation/d/1TAwxT9mRmiEjQOZurz-TbXc8SQf0mlEJDS8tL49Q-M4)

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

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

  9. GolangCI ? ʕ ◔ϖ◔ʔ

  10. GolangCI ͱ͸ • GitHub ͷ Pull Request ͷ໰୊Λݕ஌ͯ͠ίϝϯτͯ͘͠ΕΔ Golang ༻

    ͷ CI αʔϏε • ͍ΖΜͳ Linter Λ࣮ߦͯ͘͠ΕΔ • OSS ͳΒແྉͰ࢖͑Δ • Private Repo ͸1Ϣʔβ $20/mo • GolangCI ࣗମ΋ OSS ͳͷͰιʔε͕શ෦ެ։͞Ε͍ͯΔ • https://github.com/golangci Image From: https://golangci.com/
  11. ࣮ߦͯ͘͠ΕΔ Linter • શ 31 छྨ (19/01/31 ݱ࣌఺ ͷυΩϡϝϯτ্) •

    ΊͬͪΌ͋Δ • ࣮ࡍ͸ Linter ͕ merge ͞ΕͨΓͯ͠ΔͷͰਖ਼֬ͳछྨ਺ ͱͯ͠͸΋ͬͱগͳ͔ͬͨΓ͢Δ • جຊͷ gofmt, goimports, govet ͔Β
 errorcheck ͳͲҰൠతʹ࢖ΘΕͯͦ͏ͳ΋ͷ͸໢ཏ͞Ε ͯΔ͸ͣ
  12. ࣮ߦΠϝʔδ

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

  14. ద༻͢Δ Linter ͷΧελϚΠζ • .golangci.yml ʹ৭ʑॻ͍ͯ͋͛Δ • σϑΥϧτͩͱແޮʹͳ͍ͬͯΔ Linter ͕݁ߏଟ͍ͷͰ஫ҙ

    • https://github.com/golangci/golangci- lint#enabled-by-default-linters
  15. Ͱ΋ Linter ͸ CI ͚ͩͰ͸ͳ͘ ϩʔΧϧͰ΋ಈ͔͍ͨ͠ (push ͠ͳ͍ͱ݁ՌݟΕͳ͍ͷ͸ͩΔ͍)

  16. GolangCI-Lint

  17. Golang-CI Lint • ϩʔΧϧͰ GolangCI Ͱಈ͘ Lint Λ࣮ߦͰ͖Δ • CircleCI

    ͳͲͷ CI αʔϏεʹ૊ΈࠐΉ • VSCode ͳͲͷΤσΟλͱ౷߹Ͱ͖Δ • ࣅͨΑ͏ͳ Linter ʹ gometalinter ͕ଘࡏ͢Δ͕ͦΕΑΓ΋ߴ଎ʹಈ࡞ ͢Δͷ͕ಛ௃ • ଞʹ΋৭ʑվળ͞Ε͍ͯΔ (ޙड़) • Istio ΍ kustomize Ͱ࢖ΘΕ͍ͯͨΓ͢Δ
  18. gometalinter ͱͷൺֱ From: https://github.com/golangci/golangci-lint#golangci-lint-vs-gometalinter

  19. gometalinter ͱͷൺֱ • 2 ~ 7ഒ͘Β͍଎͍ • 26%͘Β͍ϝϞϦফඅ͕গͳ͍ • ฏߦॲཧ਺ͪΌΜͱ੍ޚͰ͖Δ

    • ग़ྗϑΥʔϚοτΛΧελϚΠζͰ͖Δ • আ֎ϦετͳͲΛ࢖ͬͯҰൠతͳޡݕ஌ʹରԠ • Git ͷεςʔδϯά, τϥοΩϯά͞Εͯͳ͍ϑΝΠϧͷΈʹ࣮ߦ,
 ಛఆͷϦϏδϣϯҎ߱ͷίϛοτʹର࣮ͯ͠ߦΈ͍ͨͳઃఆ͕Ͱ͖ΔͷͰେن໛ͳίʔυ ϕʔεʹ΋ಋೖ͕Ͱ͖Δ • Linter ͷݸผͷΠϯετʔϧ͕ෆཁͰ GolangCI-Lint ͚ͩΠϯετʔϧ͢Ε͹Α͍ • YAML Ͱઃఆ͕Ͱ͖Δ From: https://github.com/golangci/golangci-lint#golangci-lint-vs-gometalinter
  20. gometalinter ͱͷൺֱ From: https://github.com/golangci/golangci-lint#comparison-with-gometalinter

  21. ΊͬͪΌൺֱͯ͠Δ

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

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

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

  25. Lint ͸ద੾ʹ࢖͏ • ݫ͗͢͠Δ Lint ͸ෛ୲ʹͳΔ • ಛʹ CI ͷϫʔΫϑϩʔʹ૊Έࠐ·Ε͍ͯΔ৔߹

    ͸ڧ੍ྗ͕͋ΔͷͰ஫ҙ͢Δ • νʔϜ։ൃͷ৔߹ͳͲ͸ແཧͳ͘औΓ૊ΊΔΑ͏ɺ খ͘͞গͣͭ͠ಋೖͯ͘͠ • ద੾ͳબ୒ɺΧελϚΠζΛ͠Α͏
  26. Ͳͷ Linter Λ࢖͏໰୊

  27. @ponde_m ͷීஈ • ΘΓͱ೴ࢮͯ͠Δ • ແޮʹ͍ͯ͠Δ Linter • gochecknoglobals •

    άϩʔόϧείʔϓͷม਺͕͋ͬͨΒౖΒΕΔ • gochecknoinits • init ؔ਺͕͋ͬͨΒౖΒΕΔ
  28. ͜ͷ··ͩͱ ͋·ΓࢀߟʹͳΒͳ͍ͷͰ

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

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


    (https://speakerdeck.com/ygnmhdtt/microservices-implementation-guide-in-go-at-mercari-inc?slide=27)
  31. αΠϘ΢ζ͞Μ From: Neco ϓϩδΣΫτͷεΩϧνΣοΫγʔτ
 (https://gist.github.com/ymmt2005/bd92296166e52d1beba9df8ac516a9db)

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

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

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

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

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

    Go
 (https://golang.org/doc/effective_go.html)
 From: CodeReviewComments
 (https://github.com/golang/go/wiki/CodeReviewComments)
  37. govet • ίϯύΠϥ͕ݕग़ͯ͘͠Εͳ͍ΤϥʔΛࢦఠͯ͘͠ΕΔ

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

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

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

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

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

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

  44. ·ͱΊ

  45. ·ͱΊ • GolangCI-Lint ͸୯ମͰ·ͱΊͯ Linter Λ࣮ߦ Ͱ͖ΔΠέͯΔπʔϧ • Linter ͸ద੾ʹઃఆ͢Δ

    • ಋೖ͸খ͘͞গͣͭ͠ • ݸਓɺνʔϜʹ߹ΘͤͨΧελϚΠζΛ
  46. ͓͠·͍ ʕ ◔ϖ◔ʔ