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

dragon-importsで爆速goimports生活 - プロジェクト Go module...

dragon-importsで爆速goimports生活 - プロジェクト Go modules -/dragon-imports

Fukuoka.go#13+Okayama.go #fukuokago
https://fukuokago.connpass.com/event/112073/

monochromegane

February 12, 2019
Tweet

More Decks by monochromegane

Other Decks in Programming

Transcript

  1. - ϓϩδΣΫτ Go modules - ࡾ୐༔հ / Pepabo R&D Institute,

    GMO Pepabo, Inc. 2019.02.12 Fukuoka.go#13+Okayama.go dragon-importsͰ ര଎goimportsੜ׆
  2. 1. Motivation 2. Problem of goimports 3. Approach by dragon-imports

    4. Evalution 5. Discussion about Go modules 3 ໨࣍
  3. 9 goimportsͷ࣮ߦྫ $ cat imp.txt package imp func hoge() {

    fmt.Println("hello.") kaburaya.Recorder } $ goimports imp.txt package imp import ( "fmt" "github.com/monochromegane/kaburaya" ) func hoge() { fmt.Println("hello.") kaburaya.Recorder }
  4. goimportsඪ४ύοέʔδ 11 package imports var stdlib = map[string]map[string]bool{ "archive/tar": map[string]bool{

    "ErrFieldTooLong": true, "ErrHeader": true, "ErrWriteAfterClose": true, "ErrWriteTooLong": true, $GOROOT/api/go1.*txt golang.org/x/tools/imports/mkstdlib.go golang.org/x/tools/imports/zstdlib.go golang.org/x/tools/cmd/goimports.go#main golang.org/x/tools/imports/imports.go#Process golang.org/x/tools/imports/fix.go#addStdlibCandidates pkg crypto/rsa, method (*PrivateKey) Size() int pkg crypto/rsa, method (*PublicKey) Size() int
  5. • First pass (Πϯϙʔτ͕ඞཁ͔ௐ΂Δ) • ର৅಺ͷϦϑΝϨϯεҰཡͱimportߦ͕߹க͢Δ͔֬ೝ • Second pass (ඪ४ύοέʔδ·ͨ͸طimportߦ͔Βղܾ)

    • var stdlib·ͨ͸ଞϑΝΠϧͰimportࡁΈͷߦΛީิͱͯ͠߹க͢Δ͔֬ೝ • Third pass (֎෦ύοέʔδ͔Βղܾ) • $GOPATH/src΋͘͠͸$GOPATH/pkg/mod͔ΒϦϑΝϨϯεʹࣅͨσΟϨΫτϦΛߜΓࠐ ΈɼީิϑΝΠϧΛղੳͯ֬͠ೝ 12 goimportsͷߴ଎Խͷ޻෉
  6. 13 ஈ֊͝ͱͷ࣮ߦ଎౓ TUQBTT ЖT OEQBTT ЖT SEQBTT ЖT Πϯϙʔτෆཁ 

      ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর    ଞϑΝΠϧͷΠϯϙʔτΛࢀর    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর      $ tree !"" imp.txt !"" other.go #"" other2.go $ find . -type d | wc -l 120840 Original goimports
  7. • monochromegane/dragon-imports • A tool for speedup go imports command

    • https://github.com/monochromegane/dragon-imports • ඪ४ύοέʔδͱ$GOPATH/srcΛղੳ͠ɼzstdlib.goΛ࠶ੜ੒ɼgoimportsͷ ࠶Πϯετʔϧ·Ͱߦ͏ • zstdlib.go(=var stdlib)ͷ෼཭౓ͷߴ͞ʹΑΓ௥ैੑ΍൚༻ੑ͕ߴ·Δ 16 ఏҊख๏ͷ࣮૷ $ go get github.com/monochromegane/dragon-imports/... $ dragon-imports
  8. dragon-imports 17 package imports var stdlib = map[string]map[string]bool{ "archive/tar": map[string]bool{

    "ErrFieldTooLong": true, "ErrHeader": true, "ErrWriteAfterClose": true, "ErrWriteTooLong": true, $GOROOT/api/go1.*txt dragon-imports golang.org/x/tools/imports/zstdlib.go golang.org/x/tools/cmd/goimports.go#main golang.org/x/tools/imports/imports.go#Process golang.org/x/tools/imports/fix.go#addStdlibCandidates $GOPATH/**/*.go
  9. 19 ࣮ߦ࣌ؒͷൺֱ TUQBTT ЖT OEQBTT ЖT SEQBTT ЖT Πϯϙʔτෆཁ 

      ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর    ଞϑΝΠϧͷΠϯϙʔτΛࢀর    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর      Original goimports TUQBTT ЖT OEQBTT ЖT SEQBTT ЖT Πϯϙʔτෆཁ    ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর    ଞϑΝΠϧͷΠϯϙʔτΛࢀর    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর    Dragonize goimports
  10. • Go modules؅ཧͷ֎෦ύοέʔδΛࣄલղੳ͢Δ • ΦϓγϣϯʹΑͬͯղੳର৅ΛGOPATH/pkg/modʹมߋ͢ΔɽGO111MODULES=autoͷ ڍಈ͸ա౉ظͰ͋Δ͜ͱɼzstdlib.go͸WorkingDirΛ஌Εͳ͍͜ͱ͔ΒରԠ͠ͳ͍ • ֎෦ύοέʔδ͸֤ϝδϟʔόʔδϣϯ಺ͷ࠷৽ͷΈΛࣄલղੳ͢Δ • ϚΠφʔόʔδϣϯҎԼͷෳ਺όʔδϣϯ͕ଘࡏ͢Δ৔߹ɼbreaking

    change͸ͳ͍લఏͰ ඪ४ύοέʔδͱಉ༷ʹ࠷৽ͷΈରԠɽsemverະରԠͳ֎෦ύοέʔδʹ͓͍ͯ΋ຊํ਑ ʹΑͬͯޡ൑ఆ๷ࢭɼ࠷৽Խ΁ͷಈػ෇͚ͱ͢Δ • vendoring͸ࣄલղੳ͠ͳ͍ • ͦ΋ͦ΋ڞ௨Խ͞Εͨύοέʔδ܈͕૿େ͢Δ͜ͱͰҾ͖ى͜͞ΕΔ஗ԆͰ͋ΓɼϓϩδΣ Ϋτ͝ͱͷ࠷খͷύοέʔδ୳ࡧʹΑΔ஗Ԇ͸ߟ͑ʹ͍ͨ͘Ί 22 Go modulesΛͲ͏ѻ͏͔
  11. 23 dragon-imports $ go get -u github.com/monochromegane/dragon-imports/... $ dragon-imports --gomodules

    • gomodulesΦϓγϣϯΛ࣮ݧతʹఏڙ • Go1.12ͷਖ਼ࣜαϙʔτͷঢ়گΛݟͯΦϓγϣϯॳظ஋Λtrueͱ͢Δ༧ఆ • ܅΋ࠓ͙͢go get -u !!!
  12. • Go modulesͰ͸v2Ҏ߱ͷϝδϟʔόʔδϣϯ͝ͱʹΠϯϙʔτύε͕ҟͳΔ • ͔͠͠ύοέʔδύε͸ϝδϟʔόʔδϣϯΛؚ·ͳ͍ 24 v2ͱ͍͏ࢗ٬ $ tree !""

    hellomod $ #"" [email protected] #"" [email protected] package main import "github.com/donvito/hellomod/v2" func main() { hellomod.SayHello("Melvin") } • ඪ४ϥΠϒϥϦ͸όʔδϣϯΛߟྀ͠ͳ͍ͨΊ “v2”ͱ“hellomod”Λൺֱ͞Ε ͯ͠·͍ɼ֎෦ύοέʔδ୳ࡧ͕࢝·ͬͯ͠·͏……………………………!!!
  13. • dragon-importsͷ࢓૊ΈΛ঺հͨ͠ • UsefulͳΕͲGo modules࣌୅ʹ͸׬શରԠ͸ݫͦ͠͏ • ७ਮʹύοέʔδ͚ͩͷσΟϨΫτϦͱͳΔͷͰ஗Ԇ΋ൃੜ͠ͳ͍ͱࢥΘΕ Δɽ͠͹Β͘ӡ༻ͯ͠ࠔͬͨΒdragon-import.v2Λߟ͑Δɽ • Go

    modules࣌୅ͷศརπʔϧ΋࡞͍ͬͯ͜͏ • ͱΓ͋͑ͣGo modulesʹدͤΔʹ౰ͨͬͯҰཡΛऔಘ͢ΔͨΊͷ
 ’mole(౔ཽ)’Λ࡞ͬͯΔ https://github.com/monochromegane/mole 26 ·ͱΊ