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

gostyle IS NOT Go Style / Fukuoka.go#19 Reboot

gostyle IS NOT Go Style / Fukuoka.go#19 Reboot

Ken’ichiro Oyama

December 18, 2023
Tweet

More Decks by Ken’ichiro Oyama

Other Decks in Technology

Transcript

  1. 7 Goʹ͓͚ΔίʔσΟϯάελΠϧ go fmt • GoެࣜͷϑΥʔϚολ • ४ެࣜͱͯ͠golang.org/x/tools/cmd/goimports΋͋Δ • ιʔείʔυͷ੔ܗͯ͘͠ΕΔ

    • ʮΠϯσϯτ͸Կʹ͢Δ͔ʯͱ͍ͬͨʮύʔΩϯιϯͷຌଏ๏ଇ a.k.a ࣗసंஔ͖৔ͷٞ࿦ʯΛ ͳ͘͢͜ͱʹߩݙ͍ͯ͠Δ • ެ͕ࣜఏڙ͍ͯ͠Δͷ͜Ε͚ͩͰOK • ʹͳΔ͸ͣͩͬͨ
  2. 8 Goʹ͓͚ΔίʔσΟϯάελΠϧ go fmtͰΧόʔͰ͖Δൣғ͸ݶఆతʢͰ΋͋Δʣ • go fmtͰΧόʔͰ͖Δൣғ͸ίʔυͷϑΥʔϚοτ·Ͱ • ʮࣗసंஔ͖৔ͷٞ࿦ʯʹͳΔ༨஍͸࢒೦ͳ͕Β·ͩ͋Δ •

    ·ͨɺ1ͭͷϓϩμΫτ಺ʹҟͳΔελΠϧ͕ࠞࡏ͢Δͱೝ஌ෛՙ͕େ͖͘ͳΔͷ΋ࣄ࣮ • ΍͸ΓνʔϜʹίʔσΟϯάελΠϧ͸ඞཁͳͷͰ͸ͳ͍͔ • ίʔυϕʔεΛ؅ཧ͠΍͘͢͢ΔͨΊ • ೝ஌ෛՙΛܰݮ͢ΔͨΊ • ʢ݁Ռͱͯ͠ͷʣόάͷൃੜΛܰݮ͢ΔͨΊ • ϨϏϡʔෛՙΛܰݮ͢ΔͨΊʢػցʹͰ͖Δ͜ͱ͸ػցʹ೚ͤͨํ͕͍͍ʣ
  3. 11 Goʹ͓͚Δ͍Ζ͍ΖͳίʔσΟϯάελΠϧ Go Code Review Comments • https://go.dev/wiki/CodeReviewComments • ݩʑ͸

    https://github.com/golang/go/wiki/CodeReviewComments ʹ͕͋ͬͨɺ͍ͭ2023೥ 12݄14೔ʹҠಈ͞Εͨ • https://github.com/golang/go/issues/61940 • GoίʔυͷϨϏϡʔதʹΑ͋͘ΔίϝϯτΛ͋ͭΊͨҰൠతͳελΠϧͷཏྻʢελΠϧΨΠ υͰ͸ͳ͍ʣ
  4. 12 Goʹ͓͚Δ͍Ζ͍ΖͳίʔσΟϯάελΠϧ Go Style in Google Style Guide • https://google.github.io/styleguide/go/

    • Google͕؅ཧ͢ΔελΠϧΨΠυͱͦΕʹ෇ਵ͢ΔυΩϡϝϯτ • GoogleͰGoͷίʔυΛϨϏϡʔ͢Δਓ͕༩͑ΔελΠϧΨΠμϯεΛ౷Ұ͢Δ໾ׂ΋࣋ͭ • Style GuideɺStyle DecisionsɺBest Practicesͷ3ͭͷυΩϡϝϯτ͔ΒͳΔ
  5. 13 Goʹ͓͚Δ͍Ζ͍ΖͳίʔσΟϯάελΠϧ Uber Go Style Guide • https://github.com/uber-go/guide/blob/master/style.md • Uber͕؅ཧ͢ΔελΠϧΨΠυ

    • UberͰGoͷίʔυΛॻ͘ͱ͖ͷʮ΍Δ΂͖͜ͱʯͱʮ΍ͬͯ͸͍͚ͳ͍͜ͱʯΛৄ͘͠આ໌͢ Δ͜ͱ͕ΰʔϧ • ΤϯδχΞ͕GoݴޠͷػೳΛੜ࢈తʹ࢖༻Ͱ͖ΔΑ͏ʹ͠ͳ͕ΒɺίʔυϕʔεΛ؅ཧ͠ ΍͘͢อͭͨΊʹଘࡏ
  6. 15 Goʹ͓͚ΔLinter GolintʢDeprecatedʣ • https://github.com/golang/lint • ͔ͭͯͷGoެࣜLinter • ίʔσΟϯάελΠϧͷͨΊͷLinter •

    Effective GoͱCodeReviewCommentʹॻ͔Ε͍ͯΔελΠϧͷ׳शʹ͍ͭͯͷνΣοΫͷ ΈΛείʔϓͱ͍ͯ͠Δ
  7. 17 Goʹ͓͚ΔLinter golangci-lint • https://github.com/golangci/golangci-lint • ෳ਺ͷLinterΛ·ͱΊ࣮ͯߦͰ͖ΔLinter runner • .golangci.yml

    ͰઃఆมߋՄೳ • GitHub ActionsͷAction΋༻ҙ͞Ε͍ͯΔͳͲCIͱͷ࿈ܞ΋͠΍͘͢ɺ࣮ߦ଎౓΋͸΍͘ɺෳ਺ ͷLinterΛ੍ޚͰ͖ΔͷͰΑ͘࢖ΘΕ͍ͯΔͷΛΈΔ
  8. 18 Goʹ͓͚ΔLinter revive • https://github.com/mgechev/revive • Golintͷ୅ସ+α • ͢Ͱʹଟ͘ͷಠࣗϧʔϧ͕࣮૷͞Ε͍ͯΔ •

    TOMLͰઃఆมߋՄೳ • Golintͱಉ͡ઃఆ͕Ͱ͖Δ • golangci-lint͔Βݺͼग़͢͜ͱ΋Մೳ
  9. 19 Goʹ͓͚ΔLinter طଘLinterॴײ • ʮόάΛੜΈͦ͏ͳٙΘ͍͠ίʔυΛൃݟ͢ΔͨΊͷLinterʯͱʮίʔσΟϯάελΠϧΛ νΣοΫ͢ΔͨΊͷLinterʯ͕ଘࡏ͢Δ • golangci-lintʹ͓͍ͯ͸ࠞࡏ͍ͯ͠Δ • ίʔσΟϯάελΠϧΛνΣοΫ͢ΔͨΊͷLinterʹ͓͍ͯɺଟ͘ͷϧʔϧ͕͋Δ͕ϦϑΝϨϯ

    εͱͯ͠Կ͔͠ΒͷίʔσΟϯάελΠϧυΩϡϝϯτͱඥ͍͍ͮͯΔ΋ͷ͹͔ΓͰ͸ͳ͍ • Linterಠࣗͷ΋ͷ͕ͭ͘ΒΕ͍ͯΔ͜ͱ͕ଟ͍ • νʔϜʹίʔσΟϯάελΠϧΛνΣοΫ͢ΔLinterΛಋೖ͢Δࡍ͸ɺυΩϡϝϯτͱඥ͍ͮͯ ͍Δ΄͏͕ಋೖ͕͠΍͍͔͢΋͠Εͳ͍ʢίϯηϯαεͷͱΓ΍͢͞ʣ
  10. 21 gostyle gostyle • https://github.com/k1LoW/gostyle • ίʔσΟϯάελΠϧΛνΣοΫ͢ΔͨΊͷLinter • go vetͷ࢓૊Έͷ্ʹߏங͍ͯ͠Δ

    • go vet -vettool=`which gostyle` -gostyle.config=$PWD/.gostyle.yml ./... • ࣍ͷίʔσΟϯάελΠϧυΩϡϝϯτΛϕʔεʹAnalyzerΛ࡞੒ • Effective Go • Go Code Review Comments • Go Style in Google Style Guide • YAMLͰઃఆมߋՄೳ • //nostyle:dontpanic ͳͲͷίϝϯτͰϞμϯͳLinterͱಉ͡Α͏ʹࡉ͔͘Ignore͕Մೳ
  11. gostyle Analyzers of gostyle (18 analyzers in v0.18.4) • Effective

    Go • ifacenames ... based on https://go.dev/doc/effective_go#interface-names • Go Style in Google Style Guides • Guide • mixedcaps ... based on https://google.github.io/styleguide/go/guide#mixed-caps • Decisions • funcfmt ... based on https://google.github.io/styleguide/go/decisions#function-formatting • getters ... based on https://google.github.io/styleguide/go/decisions#getters • nilslices ... based on https://google.github.io/styleguide/go/decisions#nil-slices • pkgnames ... based on https://google.github.io/styleguide/go/decisions#package-names • recvnames ... based on https://google.github.io/styleguide/go/decisions#receiver-names • recvtype ... based on https://google.github.io/styleguide/go/decisions#receiver-type • repetition ... based on https://google.github.io/styleguide/go/decisions#repetition • typealiases ... based on https://google.github.io/styleguide/go/decisions#type-aliases • underscores ... based on https://google.github.io/styleguide/go/decisions#underscores • useany ... based on https://google.github.io/styleguide/go/decisions#use-any • useq ... based on https://google.github.io/styleguide/go/decisions#use-q • varnames ... based on https://google.github.io/styleguide/go/decisions#variable-names • Go Code Review Comments in Go wiki • contexts ... based on https://go.dev/wiki/CodeReviewComments#contexts • dontpanic ... based on https://go.dev/wiki/CodeReviewComments#dont-panic • errorstrings ... based on https://go.dev/wiki/CodeReviewComments#error-strings • handlerrors ... based on https://go.dev/wiki/CodeReviewComments#handle-errors
  12. 24 gostyleͷ࣮૷ go vetʹ౉͢vetίϚϯυͷ࡞Γํ • go vetͷ-vettoolͷϑϥάʹ౉͞ΕΔϓϩάϥϜ • ʢ1ͭͷํ๏ͱͯ͠ʣmain()Ͱ golang.org/x/tools/go/analysis/unitchecker.Main()

    Λ࣮ߦ͢Δ͜ͱͰ࣮૷Ͱ͖Δ • vetπʔϧ͸1ͭҎ্ͷgolang.org/x/tools/go/analysis.Analyzer͔Βߏ੒͞ΕΔ • ͜ͷAnalyzer1ͭ1͕ͭ੩తղੳΛͯ͠Կ͔͠ΒͷνΣοΫ͢ΔػೳΛ࣋ͭ • go vetͷσϑΥϧτͷڍಈ΋vetίϚϯυͱͯ͠࡞ΒΕ͍ͯΔʢmain()Ͱෳ਺ͷgolang.org/x/tools/go/ analysis.AnalyzerΛҾ਺ʹgolang.org/x/tools/go/analysis/unitchecker.Main()Λ࣮ߦ͍ͯ͠Δʣ • https://cs.opensource.google/go/go/+/refs/tags/go1.21.5:src/cmd/vet/main.go • https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/vet/ vetflag.go;l=71-73;drc=e44b8b15b19058b7a22a859ab4159f924856f688
  13. 25 gostyleͷ࣮૷ Example: src/cmd/vet/main.go • unitcheker.Main() ؔ਺ΛϏϧτΠϯͷ *analysis.AnalyzerΛҾ਺ʹ࣮ߦ͍ͯ͠Δ͚ͩ • unitcheker.Main()͕ϑϥά΋ؚΊͯ͢΂ͯྑ͖ʹܭ

    Βͬͯ͘ΕΔ • ͭ·ΓɺvetίϚϯυͷ࣮ଶ͸ *analysis.Analyzer • gostyle΋ಉ༷ʹෳ਺ͷ *analysis.Analyzer ͷू߹ମ
  14. 26 gostyleͷ࣮૷ analysis.AnalyzerͷϑΟʔϧυʢanalysis.AnalyzerͰͰ͖Δ͜ͱʣ • Name: ໊લΛઃఆͰ͖Δ • ϑϥάͷ໊લͳͲʹ࢖ΘΕΔ • Doc,

    URL: આ໌΍URLΛઃఆͰ͖Δ • Flags: ઐ༻ͷϑϥάΛઃఆͰ͖Δ • Run: analysis.Pass Λड͚औͬͯղੳΛ࣮ߦ͢Δؔ਺ΛઃఆͰ͖ΔʢϝΠϯͷॲཧʣ • Requires: ґଘ͢ΔAnalyzerΛઃఆͰ͖Δ • ґଘ͢ΔAnalyzer͔Βͷ݁ՌΛ analysis.Pass.ResultOf[analysis.Analyzer] Ͱड͚औΔ͜ͱ͕ Ͱ͖Δ • ʢݸਓͷײ૝ʣgithub.com/spf13/cobra.Commandʹࣅ͍ͯΔ
  15. • gostyle IS NOT Go Style • ϦϑΝϨϯεͱͳΔυΩϡϝϯτ͕͋ΔίʔσΟϯάελΠϧͷLinterΛ࡞͍ͬͯΔ • ϓϩδΣΫτ͝ͱͷLinter͕͋ͬͯ΋ྑ͍ʢ͠ɺ࣮ࡍʹ࡞͍ͬͯͨΓ͢Δʣ

    • AnalyzerΛ·ͱΊͯ unitcheker.Main() Λ࣮ߦ͢Δ͚ͩͰ΋OK • ͱ͜ΖͰΈͳ͞ΜνʔϜͷίʔσΟϯάελΠϧͲ͏͍ͯ͠·͢ʁ 31 ͓ΘΓʹ ͓ΘΓʹ