Gunosy APIチームGo開発フロー

42a3fcdaaa492f3e1cf0fe7e6bbf4251?s=47 kyokomi
March 11, 2015

Gunosy APIチームGo開発フロー

Gunosy.go#11の発表内容です

42a3fcdaaa492f3e1cf0fe7e6bbf4251?s=128

kyokomi

March 11, 2015
Tweet

Transcript

  1. Gunosy'APIνʔϜ Go։ൃϑϩʔ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 1

  2. ࣗݾ঺հ • Twi%er:)@k_yokomi)ʢ͖ΐ͜Έʣ※1 • 2015೥1݄ʹGunosy΁స৬ • GoΤϯδχΞ΍ͬͯ·͢ • Ҏલ͸ɺौ୩ͷ๭ήʔϜձࣾͰ Cocos2d;xΛ࢖ͬͨεϚϗήʔϜ։ൃ

    • ࠷ۙgo:generate͕ϚΠϒʔϜ ※1"ΞΠίϯ͸݄ඣͷᘡᘚ͞ΜͰ͢ɻ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 2
  3. ໨࣍ • ֓ཁ • ։ൃϑϩʔ • ࠓޙʹ͍ͭͯ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 3

  4. ֓ཁ Goͷ։ൃϑϩʔ಺ͰɺͲΜͳπʔϧ΍ٕज़Λ࢖͍ͬͯΔͷ͔౳ɺ Ұൠతͳ։ൃϑϩʔʹͦͬͯॱ൪ʹ঺հ͠·͢ɻ ର৅ऀ: • ͜Ε͔ΒGoΛಋೖ͠Α͏ͱ͍ͯ͠Δ • ͢Ͱʹಋೖ͍ͯͯ͠ଞͰ͸ͲͷΑ͏ʹ։ൃ͍ͯ͠Δ͔஌Γ͍ͨ • ৽نΞαΠϯͨ͠ϝϯόʔ

    @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 4
  5. ։ൃϑϩʔʢ֓ཁʣ • ؀ڥߏஙɾΤσΟλ • ίʔσΟϯάɾ࣮૷ɾςετ • εςʔδϯά؀ڥɾσϓϩΠઓུ • ͦͷଞʢϥΠϒϥϦબఆɺύοέʔδ/ϑΝΠϧ෼͚ʣ •

    ӡ༻ɾ؂ࢹ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 5
  6. ؀ڥߏஙɾΤσΟλ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 6

  7. ؀ڥߏங • Go؀ڥ͸ࣄલʹbrew౳ͰߏஙࡁΈͱ͍͏લఏ • ϓϩδΣΫτͷREADME.mdʹ؀ڥߏஙखॱΛهࡌ͍ͯ͠Δ • PC͸Mac͕ଟ͍͕ɺUbuntuͰ։ൃ͍ͯͨ͠ϝϯόʔ΋͍ͨ • ؀ڥͷҧ͍Ͱͷτϥϒϧ͕ى͖ͨέʔε͸ࠓͷͱ͜Ζͳ͍ •

    ࠓͷͱ͜ΖWindowsͰ։ൃ͍ͯ͠Δਓ͸͍ͳ͍ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 7
  8. ΤσΟλ • IntelliJ)IDEA)Community)Edi3on • Sublime • Vim • Emacs gofmt

    -sͱ͔goimportsΛߦ͍ͬͯΔͷͰɺجຊతʹΤσΟλ ͷҧ͍Ͱࠔͬͨέʔε͸ͳ͍ɻ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 8
  9. IntelliJ(IDEAͰGo • ࣗ෼͸IntelliJ)IDEAਪ͠ • Community)Edi3onʢແྉʣ • ࠷ۙɺGolang)Pulginͷ։ൃ͕IntelliJຊ ՈͷਓʁʹมΘͬͨΒ͍͠ • ver0.9ͷࠒͱ͘Β΂ͯɺ։ൃ΋׆ൃʹ

    ͳΓɺ͔ͳΓ࢖͍উखྑ͘ͳͬͨ • packageิ׬ɾίʔυิ׬ɾࢀরݩݕࡧ ͳͲͳͲ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 9
  10. ίʔσΟϯάɾ࣮૷ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 10

  11. ϩʔΧϧͰͷಈ࡞֬ೝ • go test͢Δ • ΋͘͠͸build࣮ͯ͠ߦ͢Δ • gdb౳ͷσόοΨ͸جຊతʹ࢖͍ͬͯͳ͍ • printσόοάͰे෼

    • MySQL,Redis,MongoDB౳͸ɺϩʔΧϧͷ΋ͷΛར༻ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 11
  12. ίʔσΟϯάΨΠυ ࠓ͸ɺίʔσΟϯάΨΠυ͸ͳ͍ɻҎԼ͘Β͍Ͱे෼ͳೝࣝɻ • A tour of goΛͻͱ௨Γ΍͓ͬͯ͘ • Effec&veGoΛҰಡ͢Δ •

    ӏࣂจහ͞ΜͷʮGoʹೖͬͯ͸Goʹै͑ʯΛҰಡ͢Δ※2 • golintͰͷfiled໊͸ରԠ͢ΔʢId34>3ID,3hogeUrl34>3hogeURL౳ʣ ※2"h$p:/ /ukai,go,talks.appspot.com/2014/gocon.slide#1 @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 12
  13. ςετ • guregu/mogiΛ࢖ͬͯsql͸mockͯ͠Δ • Redis΍MongoDB͸ɺϩʔΧϧͷtest༻ʹ઀ଓઌΛม͍͑ͯΔ • Handlerͷtestʹ͸ɺී௨ʹnet/h8ptestΛ࢖ͬͯΔ • tes:ngϑϨʔϜϫʔΫ͸࢖ͬͯͳ͍ʢGunosy.go#10Λࢀর※3ʣ ※3"Gunosy.go#10"testnight"h2p:/

    /gunosygo.connpass.com/event/8485/ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 13
  14. εςʔδϯά؀ڥɾσϓϩΠઓུ ͔͍͍ͬ͜deployϑϩʔ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 14

  15. @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 15

  16. ͦͷଞ ʢϥΠϒϥϦબఆɺύοέʔδ/ϑΝΠϧ෼͚ʣ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 16

  17. ϥΠϒϥϦબఆ • Ͱ͖Δ͚ͩgopkg.inͰόʔδϣϯݻఆ͍ͯ͠Δ΋ͷΛར༻ • Godep౳ͷύοέʔδϥΠϒϥϦ͸ར༻͍ͯ͠ͳ͍ • ಋೖ͢Δࡍʹ୲౰ऀ͕ͪΌΜͱௐࠪɾݕ౼ͯ͠ଞͷϝϯόʔͱ ૬ஊ͔ͯ͠ΒೖΕΔ • ΋͘͠͸ࣗ࡞͢Δ

    • ֎ʹग़ͤΔ΋ͷ͸ɺͲΜͲΜϥΠϒϥϦԽͯ͠GitHubͰެ։ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 17
  18. GitHubެ։͍ͯ͠ΔϥΠϒϥϦ ओͳϥΠϒϥϦΛൈਮʢଞʹ΋৭ʑ͋Γ·͢ʣ - gopkg.in/guregu/null.v2: nullՄೳSQLΛѻ͏ - github.com/guregu/toki: SQLTIMEܕͷnull஋Մೳ - github.com/guregu/buildver:

    build versionʢ4.1.10.5ʣͱ͔Λѻ͏ - github.com/guregu/mogi: sqlͷελϒ - github.com/guregu/kami: gojiϕʔεͷWebϑϨʔϜϫʔΫ - github.com/kyokomi/goebi: errbitΫϥΠΞϯτ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 18
  19. ύοέʔδ෼͚ɺιʔεϑΝΠϧ෼͚ • ࣾ಺ڞ௨ύοέʔδΛผϦϙδτϦʹཱ͍ͯͯΔ:# github.com/gunosy/go • σΟϨΫτϦߏ੒ͱ͍͏͔ύοέʔδ෼͚͸ະͩʹ՝୊ • ͜Εͷ࣌͸͜͏͍͏ߏ੒͕͍͍ͱ͍ܾͬͨఆ൛͕·ͩͳ͍ • ύοέʔδ෼͚ɺϑΝΠϧ෼͚ͷ஌ݟΛڞ༗͢Δษڧձ΍Γ

    ͍ͨͱࢥͬͯΔ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 19
  20. ӡ༻ɾ؂ࢹ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 20

  21. ӡ༻ɾ؂ࢹ • panicͷHandlerͰpanicͨ͠Βɺmail௨஌͍ͯ͠ΔʢerrbitҠߦ͠ ͨͷͰ΍ΊΔ༧ఆʣ • ඪ४ग़ྗΛlogϑΝΠϧʹग़ྗ • ࠷ۙɺerrbitΛಋೖͯ͠Τϥʔ؂ࢹ͢ΔΑ͏ʹͨ͠ • pprofͷ಺༰

    @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 21
  22. @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 22

  23. @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 23

  24. ࠓޙʹ͍ͭͯ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 24

  25. ίʔσΟϯάΨΠυ࡞੒ ࣾ಺ίʔσΟϯάΨΠυͷ࡞੒Λߟ͍͑ͯΔɻ • ྑ͘ͳ͍ྫ • ϨϏϡʔͰࢦఠ͕͋ͬͨ΋ͷ ΨΠυͱ͍͏΄Ͳͷ΋ͷͰ͸ͳ্͍͕هͷΑ͏ͳ΋ͷΛݟ΍͘͢ ͯ͠·ͱΊ͓͚ͯͩ͘Ͱ΋Αͦ͞͏ɻ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 25

  26. υΩϡϝϯτ؅ཧ Qiita:Teamͱ͔wikiͱ͔ʹॻ͘ͱɺ͍͍ͪͪ։͔ͳ͍ͱ͍͚ͳͯ͘ ߋ৽͕ਏ͍ɻ APIυΩϡϝϯτͷࣗಈੜ੒ʢGoDocϕʔεʣ ݱࡏਐߦதʢࠓ͸kamiઐ༻͕ͩɺ70%͘Β͍Ͱ͖ͯΔʣ (github.com/kyokomi/renkin(※4 ※4"h$ps:/ /github.com/kyokomi/renkin @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 26

  27. package main import ( "fmt" "net/http" "strconv" "github.com/guregu/kami" "golang.org/x/net/context" )

    func main() { kami.Get("/foo/:id", fooHandler) kami.Get("/bar/:name", barHandler) } // fooHandler IDΛࢦఆͯ͠FooͳΜ͔औಘ͢ΔAPI func fooHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { idStr := kami.Param(ctx, "id") // UserΛҰҙʹࣝผ͢ΔID numStr := r.FormValue("num") // ΞϨͷ݅਺ʢ1ʙ10ʣ key := r.FormValue("key") // ͦΕΛऔಘ͢ΔKey userID, err := strconv.Atoi(idStr) num, _ := strconv.Atoi(numStr) fmt.Println(userID, err, num, key) } // barHandler NameΛࢦఆͯ͠BarͳΜ͔औಘ͢ΔAPI func barHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { // hogehoge } @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 27
  28. // NOTE: THIS FILE WAS PRODUCED BY THE // RENKIN

    APIDOC GENERATION TOOL (github.com/kyokomi/renkin) // DO NOT EDIT /* RENKIN APIDOC GENERATION TOOL (https://github.com/kyokomi/renkin). Get fooHandler ɾɾɾ IDΛࢦఆͯ͠FooͳΜ͔औಘ͢ΔAPI Path: "/foo/:id" Params: 1. `"id"`: UserΛҰҙʹࣝผ͢ΔID (kami.Param) 2. `"num"`: ΞϨͷ݅਺ʢ1ʙ10ʣ (r.FormValue) 3. `"key"`: ͦΕΛऔಘ͢ΔKey (r.FormValue) file at example/main.go. Get barHandler ɾɾɾ NameΛࢦఆͯ͠BarͳΜ͔औಘ͢ΔAPI Path: "/bar/:name" Params: none file at example/main.go. */ package main @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 28
  29. @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 29

  30. ςετ kyokomi/renkinͷϑΣʔζ̎Λݕ౼தɻ • ֤APIͷςετίʔυͻͳܗΛgo:generateࣗಈੜ੒ • ίϐϖͯ͠গ͠௚͢खؒΛݮΒ͍ͨ͠ • εςʔδϯά؀ڥ޲͚ͷ૯߹ςετͷίʔυࣗಈੜ੒ • શAPIΛୟ͘Πϝʔδ

    @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 30
  31. ͓ΘΓ @k_yokomi,)2015)ʮGunosy)APIνʔϜ)Go։ൃϑϩʔʯ 31