Share
Embed
Transcript
32 slides
-
gostyle IS NOT Go Style খࢁ݈Ұ / GMO PEPABO inc.
2023.12.18 Fukuoka.go#19 Reboot 1 -
ࣗݾհ ٕज़෦ɹٕज़ج൫νʔϜ 2018 த్ೖࣾ খࢁ ݈Ұ Ken’ichiro Oyama গ࣮͠༻తͰখ͞ͳOSSΛॻ͘ͷ͕झຯɻ •
GitHub : k1LoW • Twitter : @k1LoW 2 -
7 Goʹ͓͚ΔίʔσΟϯάελΠϧ go fmt • GoެࣜͷϑΥʔϚολ • ४ެࣜͱͯ͠golang.org/x/tools/cmd/goimports͋Δ • ιʔείʔυͷܗͯ͘͠ΕΔ
• ʮΠϯσϯτԿʹ͢Δ͔ʯͱ͍ͬͨʮύʔΩϯιϯͷຌଏ๏ଇ a.k.a ࣗసंஔ͖ͷٞʯΛ ͳ͘͢͜ͱʹߩݙ͍ͯ͠Δ • ެ͕ࣜఏڙ͍ͯ͠Δͷ͜Ε͚ͩͰOK • ʹͳΔͣͩͬͨ -
8 Goʹ͓͚ΔίʔσΟϯάελΠϧ go fmtͰΧόʔͰ͖ΔൣғݶఆతʢͰ͋Δʣ • go fmtͰΧόʔͰ͖ΔൣғίʔυͷϑΥʔϚοτ·Ͱ • ʮࣗసंஔ͖ͷٞʯʹͳΔ༨೦ͳ͕Β·ͩ͋Δ •
·ͨɺ1ͭͷϓϩμΫτʹҟͳΔελΠϧ͕ࠞࡏ͢Δͱೝෛՙ͕େ͖͘ͳΔͷࣄ࣮ • ΓνʔϜʹίʔσΟϯάελΠϧඞཁͳͷͰͳ͍͔ • ίʔυϕʔεΛཧ͘͢͢͠ΔͨΊ • ೝෛՙΛܰݮ͢ΔͨΊ • ʢ݁Ռͱͯ͠ͷʣόάͷൃੜΛܰݮ͢ΔͨΊ • ϨϏϡʔෛՙΛܰݮ͢ΔͨΊʢػցʹͰ͖Δ͜ͱػցʹͤͨํ͕͍͍ʣ -
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ίʔυͷϨϏϡʔதʹΑ͋͘ΔίϝϯτΛ͋ͭΊͨҰൠతͳελΠϧͷཏྻʢελΠϧΨΠ υͰͳ͍ʣ -
12 Goʹ͓͚Δ͍Ζ͍ΖͳίʔσΟϯάελΠϧ Go Style in Google Style Guide • https://google.github.io/styleguide/go/
• Google͕ཧ͢ΔελΠϧΨΠυͱͦΕʹਵ͢ΔυΩϡϝϯτ • GoogleͰGoͷίʔυΛϨϏϡʔ͢Δਓ͕༩͑ΔελΠϧΨΠμϯεΛ౷Ұ͢Δׂ࣋ͭ • Style GuideɺStyle DecisionsɺBest Practicesͷ3ͭͷυΩϡϝϯτ͔ΒͳΔ -
13 Goʹ͓͚Δ͍Ζ͍ΖͳίʔσΟϯάελΠϧ Uber Go Style Guide • https://github.com/uber-go/guide/blob/master/style.md • Uber͕ཧ͢ΔελΠϧΨΠυ
• UberͰGoͷίʔυΛॻ͘ͱ͖ͷʮΔ͖͜ͱʯͱʮ͍͚ͬͯͳ͍͜ͱʯΛৄ͘͠આ໌͢ Δ͜ͱ͕ΰʔϧ • ΤϯδχΞ͕GoݴޠͷػೳΛੜ࢈తʹ༻Ͱ͖ΔΑ͏ʹ͠ͳ͕ΒɺίʔυϕʔεΛཧ͠ ͘͢อͭͨΊʹଘࡏ -
15 Goʹ͓͚ΔLinter GolintʢDeprecatedʣ • https://github.com/golang/lint • ͔ͭͯͷGoެࣜLinter • ίʔσΟϯάελΠϧͷͨΊͷLinter •
Effective GoͱCodeReviewCommentʹॻ͔Ε͍ͯΔελΠϧͷ׳शʹ͍ͭͯͷνΣοΫͷ ΈΛείʔϓͱ͍ͯ͠Δ -
17 Goʹ͓͚ΔLinter golangci-lint • https://github.com/golangci/golangci-lint • ෳͷLinterΛ·ͱΊ࣮ͯߦͰ͖ΔLinter runner • .golangci.yml
ͰઃఆมߋՄೳ • GitHub ActionsͷAction༻ҙ͞Ε͍ͯΔͳͲCIͱͷ࿈ܞ͘͢͠ɺ࣮ߦ͘ɺෳ ͷLinterΛ੍ޚͰ͖ΔͷͰΑ͘ΘΕ͍ͯΔͷΛΈΔ -
18 Goʹ͓͚ΔLinter revive • https://github.com/mgechev/revive • Golintͷସ+α • ͢Ͱʹଟ͘ͷಠࣗϧʔϧ͕࣮͞Ε͍ͯΔ •
TOMLͰઃఆมߋՄೳ • Golintͱಉ͡ઃఆ͕Ͱ͖Δ • golangci-lint͔Βݺͼग़͢͜ͱՄೳ -
19 Goʹ͓͚ΔLinter طଘLinterॴײ • ʮόάΛੜΈͦ͏ͳٙΘ͍͠ίʔυΛൃݟ͢ΔͨΊͷLinterʯͱʮίʔσΟϯάελΠϧΛ νΣοΫ͢ΔͨΊͷLinterʯ͕ଘࡏ͢Δ • golangci-lintʹ͓͍ͯࠞࡏ͍ͯ͠Δ • ίʔσΟϯάελΠϧΛνΣοΫ͢ΔͨΊͷLinterʹ͓͍ͯɺଟ͘ͷϧʔϧ͕͋Δ͕ϦϑΝϨϯ
εͱͯ͠Կ͔͠ΒͷίʔσΟϯάελΠϧυΩϡϝϯτͱඥ͍͍ͮͯΔͷ͔ΓͰͳ͍ • Linterಠࣗͷͷ͕ͭ͘ΒΕ͍ͯΔ͜ͱ͕ଟ͍ • νʔϜʹίʔσΟϯάελΠϧΛνΣοΫ͢ΔLinterΛಋೖ͢ΔࡍɺυΩϡϝϯτͱඥ͍ͮͯ ͍Δ΄͏͕ಋೖ͕͍͔͢͠͠Εͳ͍ʢίϯηϯαεͷͱΓ͢͞ʣ -
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͕Մೳ -
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 -
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 -
25 gostyleͷ࣮ Example: src/cmd/vet/main.go • unitcheker.Main() ؔΛϏϧτΠϯͷ *analysis.AnalyzerΛҾʹ࣮ߦ͍ͯ͠Δ͚ͩ • unitcheker.Main()͕ϑϥάؚΊͯͯ͢ྑ͖ʹܭ
Βͬͯ͘ΕΔ • ͭ·ΓɺvetίϚϯυͷ࣮ଶ *analysis.Analyzer • gostyleಉ༷ʹෳͷ *analysis.Analyzer ͷू߹ମ -
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ʹࣅ͍ͯΔ -
27 gostyleͷ࣮ AnalyzerΛ·͍ͨͩઃఆϑΝΠϧΛ࣮ݱ͢Δ • Analyzer.RequiresΛ׆༻͢Δ 1. ઃఆϑΝΠϧΛಡΈࠐΉ͚ͩͷAnalyzerΛ࡞Δʢconfig.Loaderʣ 2. gostyleΛߏ͢ΔAnalyzerશͯͰ config.Loader
ΛRequiresʹઃఆ͢Δ 3. analysis.Pass.ResultOf[config.Loader] ͔ΒઃఆΛड͚औͬͯ༻͢Δ -
28 gostyleͷ࣮ AnalyzerΛ·͍ͨͩઃఆϑΝΠϧΛ࣮ݱ͢Δ • Analyzer.RequiresΛ׆༻͢Δ 1. ઃఆϑΝΠϧΛಡΈࠐΉ͚ͩͷAnalyzerΛ࡞Δʢconfig.Loaderʣ 2. gostyleΛߏ͢ΔAnalyzerશͯͰ config.Loader
ΛRequiresʹઃఆ͢Δ 3. analysis.Pass.ResultOf[config.Loader] ͔ΒઃఆΛड͚औͬͯ༻͢Δ -
29 gostyleͷ࣮ ίʔυίϝϯτʹΑΔAnalyzerͷߦ୯Ґͷ੍ޚΛ࣮ݱ͢Δ • analysis.Analyzerʹ͓͚ΔνΣοΫΤϥʔanalysis.Pass.Reportanalysis.Pass.ReportfͰϨ ϙʔτ͢Δ • analysis.Pass.ReportΛ࣮ߦ͢ΔલʹίʔυίϝϯτͷఆΛ͢ΔϥούʔΛ࡞͢Δ͜ͱͰɺ ʮίʔυίϝϯτʹΑͬͯIgnore͞Ε͍ͯΕɺanalysis.Pass.ReportΛ࣮ߦ͠ͳ͍ʯ͜ͱͰ੍ ޚΛ࣮ݱ͢Δ
• https://github.com/k1LoW/gostyle/tree/main/reporter • ίϝϯτใΛऔಘ͢ΔͨΊʹ github.com/gostaticanalysis/comment/passes/ commentmap.AnalyzerΛRequiresʹೖΕ͓ͯ͘ -
• gostyle IS NOT Go Style • ϦϑΝϨϯεͱͳΔυΩϡϝϯτ͕͋ΔίʔσΟϯάελΠϧͷLinterΛ࡞͍ͬͯΔ • ϓϩδΣΫτ͝ͱͷLinter͕͋ͬͯྑ͍ʢ͠ɺ࣮ࡍʹ࡞͍ͬͯͨΓ͢Δʣ
• AnalyzerΛ·ͱΊͯ unitcheker.Main() Λ࣮ߦ͢Δ͚ͩͰOK • ͱ͜ΖͰΈͳ͞ΜνʔϜͷίʔσΟϯάελΠϧͲ͏͍ͯ͠·͢ʁ 31 ͓ΘΓʹ ͓ΘΓʹ