Perl to Go

2d524d56b2ddff49363c995f2d795da0?s=47 Yuji Shimada
January 26, 2019

Perl to Go

YAPC::Tokyo 2019 で発表した内容になります

2d524d56b2ddff49363c995f2d795da0?s=128

Yuji Shimada

January 26, 2019
Tweet

Transcript

  1. Perl to Go Yuji Shimada @xaicron YAPC::TOKYO 2019 ASAKUSABASHI HULIC

    HALL & HULIC CONFERENCE Jan, 26 2019 [SAT]
  2. Ε΀ͫΩ Go 䨗͚ͼΔͯ͡Ҙ

  3. 㰜΅݄ଙ΄Ӿ殹͡Ο䨗ͣতΗΔͭ͵

  4. ͩ΄ϕЄμͽ΅ Perl Ψ 10ଙͦΟ͚՛Ԫͽ 䨗ͣ姆ͧͼͣ͵ͮͫ͠Ω͢ Go ͽ Web API Ψ֢͹ͼ஑͵Ӟ᭗Π΄ίϹΨ࣮΢ၞͯΘ΄

    ΁΀ΠΔͯ
  5. Go ͽ WebίϤϷξЄτϴЀΨ䨗ͥၞ΢͢ ͬ͹ͥΠഽΗΡ;Ξ͚ͽͯ΃

  6. ΅͚

  7. ίυδЀύ • ΀Ͳ Go ͡ • Perl Monger ͢ Golang

    ΨতΗΞ͜͹ͼ䦒΁Ϝϫ͹͵;ͩΣ • Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • Web API Ψ֢͹ͼΕΞ͜ • ϓφϕΨ䨗ͥ • Docker Ϡϸϖͭͼ㵕ͯ͡ • Δ;Η
  8. ΀Ͳ Go ͡

  9. ΀Ͳ Go ͡ • ίυδЀύ • WebςЄϝЄ;ͭͼΕ͵ Go • πϫЀϖ϶αЀϑЄϸ;ͭͼΕ͵

    Go
  10. WebςЄϝЄ;ͭͼΕ͵ Go (1) • ฌՔ΅ηЀϤϹΨ晁አͯΡ΄΅ύγЀϕϹЀϖ΁΀͹ͼͣͼ͚ Ρ • μ϶γϖӤͽͭ͡ΘπЀϓϗӤͽίϤϷξЄτϴЀΨ㵕ͯͩ͡ ;͢୮ᆐ΁΀͹ͼ͚Ρ

  11. WebςЄϝЄ;ͭͼΕ͵ Go (2) • πЀϓϗ΅ηЀϤϹ;ྲΏΡ;涥ͥΑͿ΄֗φϧϐμ΄ςЄϝЄӤͽ㵕̵ͭ͡ හ܈뺶හጯݣ㪔ΏΡΞ͜΀ොݻ • ςЄϝЄ΄φϧϐμ͚֗͢͵Ηṛ᭛ͽ㵕̵֢ͭ͡ͺϮϯϷ㵁ሲΘΞ͚᥺承͢࿢ ΗΟ΢Ρ •

    ͫΟ΁πЀϓϗᛔ֛΄ςαχΘੜͫͥͭ͵Α͜͢Ӟᛱጱ΁᩸㵕᭛ଶ͢Ӥ͢Ρ͵ Ηੜͫͥͭ͵͚ • φμϷϤϕ᥺承΄Ξ͜΁϶Ѐόαϭ͢஠ᥝ΀Θ΄΅හጯMBΨ᩻͞ͼͭΔ͚᭿ ͧ͵͚
  12. WebςЄϝЄ;ͭͼΕ͵ Go (3) • Go ΄䁰ݳ΅ • ϝαϗϷΨ֢౮ͯ΢Ά϶Ѐόαϭ͢஠ᥝ΀͚͵ΗπЀϓϗ΄ςαχΨಪ ͞Ρͩ;͢ͽͣΡ •

    ฦ᭗΁䨗ͥͶͧͽ Java Κ C ΁܃䤔ͯΡΑͿṛ᭛͡ͺ፜ϮϯϷͽ㵕֢ͯΡ • gorutine ΁ΞΠ̵ᛔᆐ;㵁ሲጱ΁CPU;ᑮ樌ΨڥአͯΡͩ;͢ͽͣΡ • Ӥ懿΄ᇙ䖀΅ηЀϤϹͽΘ㱢֖΁㰕ͥ
  13. πϫЀϖ϶αЀϑЄϸ;ͭͼΕ͵ Go (1) • 樄咲ᘏ͡ΟͯΡ; • CPUΚϮϯϷ΄௔ᚆ͢Ӥ͢͹͵΄; go ΄πЀϞαϸᛔ֛͢ṛ ᭛΀͵Η

    LL ;晉ᜋ͢΀͚ϹϦϸͽ墋㶨΁πЄϖΨ䨗͚ͼ手ͯ ͩ;͢ͽͣΡ΄ͽφμϷϤϕఽ憝ͽπЄϖΨ䨗ͥͩ;͢਻ฃ • ل樄ͯΡ;ͣΘμϺφπЀϞαϸ͢਻ฃ΀΄ͽϝαϗϷΨኞ ౮ͭͼ晒୮΀䁰ಅ΁ίϐϤͭͼͧ͠ΆΞ͚
  14. πϫЀϖ϶αЀϑЄϸ;ͭͼΕ͵ Go (2) • ڥአᘏ͡ΟͯΡ; • ϚϐϕϼЄμ΄᭛ଶ͢ค΁ྲΏͼݻӤͭͼ͚Ρͭϔΰφμς αχΘ䃀͞ͼ͚Ρ΄ͽහ܈MB΄ϝαϗϷΨύγЀϺЄϖͯΡ ΄΅֜΄㺔氂Θ΀͚ •

    φμϷϤϕ᥺承;晅͚϶Ѐόαϭ͢஠ᥝ΀͚΄ͽϝαϗϷΨ ϪЀ;ͥ͠Ͷͧͽֵ͞ͼ׎ڥ
  15. Ӥ懿΄Ξ͜΀ቘኧ΀Ϳͽ匍դͽ͚Σ͚Σ΀ ఺ޱͽֵ͚Κ͚ͯ᥺承Ͷ;ఽͮΡ

  16. ΑΩ;΅ၞᤈ͹ͼΡ͡ΟΚ͹ͼΕt

  17. ΅͚

  18. ίυδЀύ • ΀Ͳ Go ͡ • Perl Monger ͢ Golang

    ΨতΗΞ͜͹ͼ䦒΁Ϝϫ͹͵;ͩΣ • Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • Go ͽ๋ṛ΄ Web API Ψ֢͹ͼΕΞ͜Ѻ • Go ͽ͚͚ے仂ϓφϕΨ䨗ͥʹѺ • ͚͚ఽͮ΁ϠϸϖͭͼϷϷЄφͯΡʹѺ • Δ;Η
  19. Perl Monger ͢ Golang ΨতΗΞ͜͹ͼ䦒 ΁Ϝϫ͹͵;ͩΣ • ίυδЀύ • CPAN

    ͢΀͚ • $GOPATH ͡Οᭈ΢Ο΢΀͚ • ϶αϣ϶Ϸ΄ᓕቘොဩ͢ΖͰ͚ • ηЄςϷЀνϑЄϸ͢΀͚
  20. CPAN͢΀͚ (1) • Go ΅ git repository ΄ URL Ψ೰ਧͭͼαЀφϕЄϸͯΡ

    • CPAN Κ Ruby gems Κ npm ΄Ξ͜΀Ӿ।ᵞ䰱ጱςЄϝЄ͢ ΀͚ • Ӟͺ΄;ͩΣ΁Δ;Δ͹ͼ΀͚΄ͽ͚ͩͩ͜͜;ͭ͵͚ͧͿ ΀΁͚͚͡϶αϣ϶Ϸ΀͚͡΀뺶;റͯ΄͢ࢯ櫞
  21. CPAN͢΀͚ (2) • ϶αϣ϶ϷΨറͯ;ͣ΅ • github.com ͽ golang + hogehoge

    ͯΡ • Έ͵ͯΟννΡ • ํ挷ᘏ΁守ͥ • etc…
  22. CPAN΁ၺ͡͹ͼኞͣͼͣ͵΄ͽͺΟ͚

  23. ❗ͩͩͽ๔䁭ѺѺ

  24. ๔䁭 • https://blog.golang.org/modules2019 • ਖ਼๶ጱ΁ҁ2019ଙӾҘ҂΁ Module Indexer Ψ֢ΡԨਧΟͭ ͚ѺѺΚ͹͵΃ѺѺ •

    Ք஍ CPAN Testers ጱ΀Θ΄Θͽͣͼ͚ͣͳ͜΀䶲ͯ͢Ρҁ㮆 Ոጱ΀Ԩమͽͯ҂
  25. ๔䁭 • ϶αϣ϶Ϸ΄ύγЀϺЄϖ΄ mirror Θ֢౮Ԩਧ;΄ͩ; • ͩ΢΅ Go 1.13 ͽ䌏䖕ͯΡΕ͵͚΀΄ͽ

    2019.8 ΁΅ϷϷЄ φͫ΢Ρ͹Γ͚Ҙ • ֺ͞Ά github.com ៧ͷͼΘ mirror ͡ΟύγЀϺЄϖͽͣΡ Ξ͜΁;͚ͩ͜;Ο͚ͭ
  26. minicpan ͽ CPAN mirror 䯤塈ͭͼ 䶅෭ cron ͽ ๅෛͭͼ͚͵෭̸Ψ௏͚ڊͭ Δͯ΃

  27. $GOPATH ͡Οᭈ΢Ο΢΀͚ (1) • golang ͽ΄樄咲΅ଉ΁ $GOPATH ΄ӥͽᤈ͜஠ᥝ͘͢Ρ • ͘ΟΜΡ϶αϣ϶Ϸ͢

    $GOPATH ΄ӥ΁ᗝ͡΢Ρڹ൉΁΀͹ͼ ͚ͼ͡ͺ $GOPATH ΄ᯈӥͽ樄咲ᤈΥ΀͚;ᶎׯ΀ͩ;΁΀Ρ
  28. $GOPATH ͡Οᭈ΢Ο΢΀͚ (2) • Go 1.8 զᴳͽ΅ $GOPATH ΅ϔϢζϸϕͽ $HOME/go

    ΁΀͹ ͼ͚Ρ • ͷΝ͹;;͹ͺͣΚͯͥ΀͹͵
  29. $GOPATH ͡Οᭈ΢Ο΢΀͚ (3) • Go 1.12 ͽفΡ go mod ͽ

    $GOPATH Ψ䶲΁ͭ΀ͥͼΘΞͥ΀ΡΟ͚ͭ…͢ • ͚ΔΔͽل樄ͫ΢ͼͣ͵͘ΟΜΡϯυϲЄϸ͢ semver ΁䌏䖕ͯΡ஠ᥝ͘͢Ρ • go mod ͽΘϯυϲЄϸ΄κϰϐτϲ΀Ϳ΅ $GOPATH ᯈӥ΁֢Ο΢Ρ΄ͽ ਠق΁䶲΁ͭ΀ͥͼΞͥ΀ΡΥͧͽ΅΀ͫͳ͜ • ͳΘͳΘ $GOPATH ΄ᯈӥͽͰ͹;Κ͹ͼͣ͵͡Ο嬹䙼Ψ䄜͞Ρ΄΅䦒樌͢ ͡͡Πͳ͜ • Go 1.12 ΅ 2019.2 ΁ϷϷЄφԨਧѺҁΘͯͦ͜ͽͯ΃ѺѺ҂
  30. ϶αϣ϶Ϸ΄ᓕቘොဩ͢ΖͰ͚ (1) • ϶αϣ϶Ϸ΄αЀφϕЄϸ΅ go get πϫЀϖΨֵ͜ • go get

    -u github.com/motemen/gore ΄Ξ͜΀୵ୗͽׁਂ樛㭚ΘތΗͼαЀφ ϕЄϸͭͼͥ΢Ρ • $GOPATH/src ΁ΘΣΘΣ git clone ͫ΢ͼϠϸϖ͢ᩳΡ • Քࢧ΄ξЄφͶ; $GOPATH/src/github.com/motemen/gore ΀Ϳ • πϫЀϖ϶αЀϑЄϸΘ $GOPATH/bin ΁ϠϸϖͭͼαЀφϕЄϸͭͼͥ΢Ρ • perl ͽ cpanm install Foo::Bar ͯΡ΄;Ͷ͚͵͚Ӟ姼
  31. None
  32. ϶αϣ϶Ϸ΄ᓕቘොဩ͢ΖͰ͚ (2) • go get ͽαЀφϕЄϸͫ΢Ρ΄΅ git repository ΄ master

    ΄ HEADѺ • 䋚檭΅ go1 ;͚͜ branch or tag ͘͢΢Άͳ΢Ψ憎ΡͧͿΑ Βၺ᭐ͭͼ͚΀͚ཛྷ䯭 • master ͢ stable ͡Ϳ͜͡΅Υ͡ΟΩ΄ͽ΀͡΀͡㷗͚ͭΘ΄ ͘͢Ρ
  33. ϶αϣ϶Ϸ΄ᓕቘොဩ͢ΖͰ͚ (3) • go get ͽ΅ version Ψ೰ਧͭͼݐ஑ͯΡͩ;͢ͽͣ΀͚ • ͳΘͳΘ

    version ᓕቘͫ΢ͼ͚ΡϯυϲЄϸ͢੝΀͚ѺѺ • Go 1.12 ͽفΡ go mod ΁ΞΠ semver Ψֵ͜Ξ͜΁΀Ρ΄ ͽϮЀϓͫ΢ͼ͚Ρ϶αϣ϶Ϸ΁΅ၺ᭐ͭͼ͚͹ͼ΅͚Ρ
  34. ;΅͚͞

  35. WebίϤϷξЄτϴЀ΀ͿΨ䨗ͥ䁰ݳ΅ go get Ψፗളֵͩ͜;΅च๜ጱ΁΅΀͚

  36. WebίϤϷξЄτϴЀΨ䨗ͥ;ͣ΄϶αϣ ϶Ϸ΄ᓕቘ (1) • Carton Κ bundler ΄Ξ͜΀̵ϤϺυδμϕᯈӥ΁஠ᥝ΀ϯυϲЄ ϸΨϝЄυϴЀࢴਧͭͼαЀφϕЄϸͯΡ՛奲ΕΨֵͩ͜;͢Α ;ΩͿ

    • go get Ψֵ͜΄΅ᇙਧ΄ϯυϲЄϸ΄䞉㵕Ψ抠Ώ͵͚;̵͡π ϫЀϖֵ͚͵͚;͡͹ͼ䁰ݳ͢Α;ΩͿ • ͭͭ͡ vendoring / glide / dep / vgo / go mod ;͚Σ͚Σ͘͹ͼ 奾ੴͿ΢ֵ͞Ά͚͚΄Ҙ;΀Ρ
  37. WebίϤϷξЄτϴЀΨ䨗ͥ;ͣ΄϶αϣ ϶Ϸ΄ᓕቘ (2) • ୮ᶎ΅ glide / dep / go

    mod Ψֵ͚ړͧͼ͚ͥ஠ᥝ͘͢Ρ;௏Υ΢Ρ • glide / dep ΅ֵΥ΢ͼ͚ΡϤϺυδμϕ͢ग̵़ͥΑΒΑΒ๗இ᭗Π΁㵕 ͥ΄ͽ㪥Π䟵͞ΡϯώϦЄτϴЀ֗͢ͳ͜ • vgo ΅ go mod ;ͭͼ Go ΁ݐΠ旿Δ΢͵΄ͽ஫΢ͼᜉ͚ • Go 1.12 ͡Ο΅ go mod Ψֵ͹ͼ͚ͥͩ;΁΀Ρ΅Ͱ • 匍ࣁ΄ Go 1.11 ͽ΅ $GO111MODULE =on ΁ͭͼͥ͠;͠手ͭͽͣΡ
  38. ;͚͜Υͧͽ ΔͶΔͶͩ΀΢ͼ΀͚᮱ړ͘͢Ρఽͮ

  39. ηЄςϷЀνϑЄϸ͢΀͚ (1) • Go ΁΅ηЄςϷЀνϑЄϸ΄䷥ਧᇇ΄Ξ͜΀Θ΄͢΀͚ • Δ͹ͫΟ΀य़ࣈ͡ΟতΗΡͩ;΁΀Ρ • go xxx

    πϫЀϖΨ;΁ͥ͡಑ͺ͡ Makefile Ψ;΁ͥ͡䨗ͥͩ;΁΀Ρ • Ϡϸϖ͢ͳͩΔͽ櫞ͭͥ΀͚΄ͽ̵ͳΩ΀΁ࢯΟ΀͚;͚͞ΆࢯΟ΀͚͢… • ϷϷЄφ䦒΁ tag ಑͹͵Π̵Ϡϸϖͭ͵ϝαϗϷ΁ version ईΗ旿ΩͶΠ΀Ϳਧࣳ ΄㳌ቘ΅ generate ͭ͵͚ཿ • ;ͥ΁ go mod ΁ΞΠ϶αϣ϶Ϸ΄ϝЄυϴЀᓕቘ͢᯿ᥝ΁΀͹ͼ͚ͥ
  40. ηЄςϷЀνϑЄϸ͢΀͚ (2) • ౯̸΅ Minilla Κ ExtUtils::MakeMakerҁݘ͚҂ ΁䙼΢ͯͤͼ ͭΔ͹͵ΩΚ… •

    go mod Θֵ͞ΡΞ͜΁΀Ρ̵ͭΒͷΒͷͩ΄旽΄ϑЄϸΘڊ ͼͣͼΘ͚͚殹΀䶲΅ͯΡ
  41. ഄΠᬬ͹ͼΕΡ;ΘΣΘΣ϶αϣ϶Ϸ΄䜷 ͚;͡ͽϜϫ͹ͼ͚͵

  42. ͭ͡ΘՔ஍Θ䄜Υ͹ͼ͚ͥ΄ͽ͘͹͵

  43. ΅͚

  44. ίυδЀύ • ΀Ͳ Go ͡ • Perl Monger ͢ Golang

    ΨতΗΞ͜͹ͼ䦒΁Ϝϫ͹͵;ͩΣ • Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • Web API Ψ֢͹ͼΕΞ͜ • ϓφϕΨ䨗ͥ • Docker Ϡϸϖͭͼ㵕ͯ͡ • Δ;Η
  45. Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • ίυδЀύ (1) • 䄜හ;ࣳ΁ͺ͚ͼ •

    樛හ΁ͺ͚ͼ • ε϶ЄϜЀϖϷЀν΁ͺ͚ͼ • defer ΁ͺ͚ͼ • gorutine ΁ͺ͚ͼ • context.Context ΁ͺ͚ͼ • package ΁ͺ͚ͼ
  46. Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • ίυδЀύ (2) • ګக䯤෈΁ͺ͚ͼҁif /

    for / while / switch҂ • goto ΁ͺ͚ͼ • Struct ΁ͺ͚ͼ • interface ΁ͺ͚ͼ • code formatter ΁ͺ͚ͼ • linter ΁ͺ͚ͼ
  47. 䄜හ;ࣳ΁ͺ͚ͼ (1) • 䄜හ΅ calemCase ͽਧ嬝ͯΡ • 䄜හ΁΅ࣳ͘͢Π̵Ӟଶਧ嬝ͫ΢͵Ο㳨΄ࣳ΅դفͽͣ΀͚ • ᯈڜ;φ϶αφ;͚༷͜ஷ͘͢͹ͼፗఽጱ΁ቘᥴͭͻΟ͚͢च

    ๜ጱ΁΅φ϶αφֵͭ͡Υ΀͚΄ͽ䶲΁ͭ΀ͥͼᜉ͚ • φ϶αφ΅ͬ͹ͥΠ͚͜;ݢ䄜槱΄ᯈڜΕ͵͚΀Θ΄
  48. None
  49. 䄜හ;ࣳ΁ͺ͚ͼ (2) • Perl ΅ٖ᮱ጱ΁ࣳఘ䁭ΨΘ͹ͼ͚Ρ͢΀ΩͽΘϣώ旿ΗΡ • Scalar::Util::dualvar ΀ͿΨֵ͞ΆӞͺ΄䄜හͽ愢හ΄ࣳ΄㳨 ΄㮔ΨΌͷ旿Ζͩ;Θݢᚆͽ͚ͯͪ •

    Go ΅ᶉጱࣳ՞ͧ᥺承΀΄ͽࣳ͢͡͹ͷΠͭͼ͚Ρ̵͢ग़ͥ΄ 䁰ݳͽࣳΨ䨗͚ͼࢧΟ΀ͥͼΘπЀϞα϶͢Ξͭ΀΁ͭͼͥ΢ Ρ
  50. 樛හ΁ͺ͚ͼ (1) • 樛හ΅ func (୚හ) 䜟Π㮔 { … }

    ;͚͜୵ୗͽ懿ᬿͯΡ • 樛හݷ΅ PascalCase ͽ䨗ͥ; public ;΀Π camelCase ͽ䨗ͥ ; private ΁΀Ρ • ݢ䄜槱୚හΨݑͧ՞ͧΡͩ;ΘͽͣΡ
  51. None
  52. 樛හ΁ͺ͚ͼ (2) • 樛හ΅愢හ΄㮔Ψᬬͯͩ;ΘͽͣΡ • Perl ͹Γ͚ͽͯ΃Ѻ • 樛හ΅䄜හ΁դف̵ͭ՜΄樛හ΁Ⴡͯͩ;ΘͽͣΡ •

    ΘͷΣΩ僻ݷ樛හΘ֢΢Ρ
  53. None
  54. 樛හ΁ͺ͚ͼ (3) • Perl ͽ΅樛හϷϢήϹЀφΨ䄜හ΁ف΢ͼჁͯͩ;ΘͽͣΡͧ Ϳ go ΅Ϳ͹ͷ͡;͚͜; JavaScript ͹Γ͚ఽͮ

    • ͵Ͷ̵ͭ樛හΨ୚හ΁ͭ͵Π䜟Π㮔΁ͯΡ䁰ݳ΁τνϚώϰЄ Ψق᮱䨗ͥ஠ᥝ͘͢Ρ
  55. ε϶ЄϜЀϖϷЀν (1) • go ΅ perl ;ݶͮͽ throw exception Κ

    try / cache ΄Ξ͜΀ֺक़䱛䯤 ͢΀͚ • ֺक़Ψڊͯ䁰ݳ΅ panic() Ψֵ͜ • perl ͽ΅͠΀ͮΕ΄ die ͽͯ΃Ѻ • panic() ΨκϰϐώͯΡ΄΅ defer func () { recover() } Ψֵ͜;ͽͣΡ • perl ͽ΅͠΀ͮΕ΄ eval ͽͯ΃Ѻ
  56. None
  57. ̵ͭͭ͡᭗ଉ΅ panic() Ψֵͩ͜;΅വ䅏 ͫ΢ͼ͚ΔͱΩѺѺ

  58. ε϶ЄϜЀϖϷЀν (2) • ᛘ޸ጱ΀ε϶ЄͽϤϺψφΨ奰ԧͭ΀ͧ΢Ά΀Ο΀͚䦒΁ᴴ͹ ͼֵ͜Ώͣ • ͭ͵͢͹ͼ recover() ΄ڊኾΘᛔͰ;Α;ΩͿ΀͚ •

    WebίϤϷξЄτϴЀ΀Ϳͽ΅΂ΡΓΚOutOfIndexጱ΀Θ΄ ͽ϶Ѐόαϭε϶Єͽ panic() ͢咲ኞͯΡݢᚆ௔΅͘Ρ΄ͽϷ μεφϕ΄Ӟኾक़㯎ͽ recover() ͯΡͦΟ͚
  59. ε϶ЄϜЀϖϷЀν (3) • go ͽ΅ε϶Є㮔Ψ䜟Π㮔ͽᬬͯͩ;͢വ䅏ͫ΢ͼ͚Ρ • ڹᬿͭ͵Ξ͜΁ go ΄樛හ΅䜟Π㮔Ψ愢හᬬͯͩ;͢ͽ̵ͣच ๜ጱ΁΅๋஍΄䜟Π㮔͢

    error αЀόЄϢδЄφΔ͵΅ bool ΁΀ΡΞ͜΁ͯΡ • αЀόЄϢδЄφ΁ͺ͚ͼ΅஍ᬿͭΔͯ
  60. None
  61. ͩΩ΀ఽͮͽ樛හ΅ग़ͥ΄ξЄφͽ error ΨᬬͯΞ͜΁΀͹ͼ͚Ρ΄ͽ晒ڔ΁㳌ቘͯ Ρ஠ᥝ͘͢ΠΔͯ

  62. defer ΁ͺ͚ͼ (1) • defer func() ͯΡ;匍ࣁ΄樛හ͡Ο䝏ͧΡ䦒΁ func() Ψ䋚ᤈͯΡ •

    Perl ͽ᥺͜ Scope::Guard ΁֒ͼ͚Ρ • go ΄ defer ; Scope::Guard ΄晅͚ • 䜟Π㮔͢΀͚΄ͽ̵Scope::Guard ΄ dismiss ΄Ξ͜΀ͩ;΅ͽͣ΀͚ • φπЄϤΨ䝏ͧ͵䦒ͽ΅΀̵ͥͥ͘ΔͽΘ樛හΨ䝏ͧ͵䦒΁ͭ͡咲㵕ͭ΀͚ • If Κ for ΄Ӿͽ defer Ψ䨗ͥ΄΅఺㾩ͭ͵䞉㵕;΀Ο΀͚ݢᚆ௔͢ṛ͚΄ͽဳ఺
  63. None
  64. defer ΁ͺ͚ͼ (2) • 䋚΅Ӥ懿΄πЄϖ΅ε϶ЄΨൎΠͺΌͭͼ͚Ρ • file.Close() ΅ε϶ЄΨᬬͯͩ;͘͢Ρ • ͷΙΩ;ϜЀϖϷЀνͭ΀͚;͘͡Ωͽͯ

  65. None
  66. defer ΁ͺ͚ͼ (3) • ͫΟ΁̵defer ͽ咲ኞͭ͵ε϶ЄΨ樛හ΄䜟Π㮔;ͭͼᬬͭ͵ 䁰ݳ΅Ӟૡॢ஠ᥝ΁΀Ρ • 樛හ΁ε϶Є͘͢Ρ͡Ϳ͜͡ͷΙΩ;嘦扯ͭͼ䶲Ψͺͧͼֵ͜ ஠ᥝ͘͢Ρ

  67. gorutine ΁ͺ͚ͼ • Go Ψ᨝䖀ͯΡ䱛ᚆ • `go func()` ͯΡͶͧͽ̵㳨φϹϐϖͽ㳌ቘ͢ᩳΡ΄ͽ㪔ڜϤϺ ν϶ϬЀν͢墋㶨΁ͽͣͼͭΔ͜

    • gorutine ݶॊ΄ΚΠ;Π΅ channel Ψֵͩ͜;ͽᤈ͜ • Perl ͽ͚͜ AnyEvent Κ Coro ;༷ஷ΅ΑΒӞ姼ͽͯ΃
  68. context.Context ΁ͺ͚ͼ • ᇙਧ΄ gorutine Κ Ӟ昧΄㳌ቘ΄Ӿͽձ఺΄ϔЄόΨف΢ͼ୚ͣࢧͭ͵ Π̵κϰЀψϸ㳌ቘΨူ݊ͫͱΡ͵Η΀Ϳ΁ڥአͯΡ • Perl

    ΄ Amon2 Κ Catalyst (䛖͚ͭ͡) ΀Ϳ΄ $c ;֒͵Ξ͜΀༷ஷ • go ΄䁰ݳ΅ core ΁ف͹ͼ͚Ρ͵Η̵͚Σ͚Σ΀ϯυϲЄϸͽݶͮ API Ψ ڥአͯΡͩ;͢ͽͣΡ΄ͽΗͷΙͥͷΙֵΥ΢ͼ͚Ρ • ᇙ΁WebίϤϷξЄτϴЀΨ֢ΡӤͽ΅ όαϭίγϕΚٖ᮱ε϶Єͽ㳌 ቘΨκϰЀψϸͭ͵͚䦒΀Ϳ΁஠殾΄䱛ᚆͽ;΁ͥ͡୚ͣࢧͯͩ;΁΀Ρ
  69. pacakge ΁ͺ͚ͼ (1) • go ͽϯυϲЄϸΨαЀϪЄϕͯΡ䁰ݳ΅ • core ΄䁰ݳ΅ net/http

    ΄Ξ͜΁ͯΡ • ͳ͜ͽ΀͚䁰ݳ΅ github.com/jmoiron/sqlx ΄Ξ͜΁ͯΡ
  70. pacakge ΁ͺ͚ͼ (2) • net/http Ψֵ͜䁰ݳ΅ http.Get(“…”) ΄Ξ͜΁ http Ψ

    prefix ; ͭͼֵ͜ • net/http ΨαЀϪЄϕͭͼ͚ΡΞϢήαϸͽΘͭ github.com/ xaicron/http ΄Ξ͜΀ package ΨαЀϪЄϕͭΞ͜;ͯΡ; http ΄ prefix ͢ᤩ͹ͼͭΔ͜ • ͳ΄䁰ݳ΅αЀϪЄϕ;ͣ΁㳨ݷΨͺͧΡͩ;͢ͽͣΡ
  71. None
  72. pacakge ΁ͺ͚ͼ (3) • package ΄φπЄϤ΁ͺ͚ͼ • sqlx ΄ϔΰϹμϕϷ䯤౮Ψ憎ͼΕΡ;̵Α;ΩͿ΄Ϣήαϸ ͢Ϣ϶ϐϕ΁ᗝ͡΢ͼ͚Ρ΄͢Υ͡Ρ

    • named.go ΀ͿΨΈΟͥ; package sqlx ;΀͹ͼ͚Ρ΄͢Υ ͡Ρ
  73. pacakge ΁ͺ͚ͼ (4) • Perl ΄䁰ݳ΅ `ϔΰϹμϕϷ::Ϣήαϸݷ` ͢ package ݷͶ͢晅͜

    • Go ΄䁰ݳ΅φπЄϤ΄塅㾨͢ package ΀΄ͽݶͮϔΰϹμϕ Ϸ΁͘Ρ *.go Ϣήαϸ΅قͼݶͮφπЄϤͽ͘Ρ • Perl ΅ϢήαϸφπЄϤ • Go ΄ϊЄφ΅ݶͮϔΰϹμϕϷ΄Ϣήαϸ΁㳌ቘ͢ړවͭͼ ͚Ρ΄͢ฦ᭗
  74. ϞϐξЄυ΄䜷͚͢ Perl ;΅奾䯤晅͜΄ͽ 䶲ΨͺͧΡ஠ᥝ͘͢Ρ

  75. ګக䯤෈΁ͺ͚ͼ (1) • If ΁ͺ͚ͼ • if ΁ೡୢ΅஠ᥝ΀ͭ • elseif

    ͽ΅΀ͥ else if • ӣ殻ᄍᓒৼ͢ਂࣁͭ΀͚͵Η̵ͺ΃΁ if ~ else Ψ䨗ͥ஠ᥝ͢ ͘Ρ
  76. ګக䯤෈΁ͺ͚ͼ (2) • for ΁ͺ͚ͼ • for ΁Θೡୢ΅஠ᥝ΀ͭ • for

    ΄Ӿͽ΅ break ; continue ֵ͢͞Ρ • foreach 䯤෈΅΀͚͢դΥΠ΁ for … := range var { … } 䯤෈ ֵ͢͞Ρ
  77. None
  78. while ΁ͺ͚ͼ • while ΀Ϳ;͚͹͵揞୧΀Θ΄΅΀͚ѺѺ • for ΄๵կ෈Ψ΀ͥͱΆ while ΁΀Ρ΄ͽ㺔氂΀ͭ

    • for n < 100 { n++ } ΄Ξ͜΀ఽͮ
  79. switch ΁ͺ͚ͼ • 䘂ݷṛͣ switch ෈ͽ̵ͯ͢go ͽ΅Ξֵ͚ͥΔͯ • case ΄͘;΁

    break Ψ䨗ͥ஠ᥝ͢΀͚΄ͽ఺㾩ͱͰེ΄ case ΁ᤈ͹ͼͭΔ͜;͚͜ϝν᩸ͩ͢Π΁͚ͥ • ེ΄ case ΁ᤈͣ͵͚䁰ݳ΅กᐏጱ΁ fallthrough Ψ䨗ͥ஠ᥝ ͘͢Ρ
  80. None
  81. goto ΁ͺ͚ͼ • go ΁΅ goto ͘͢ΡѺΚ͹͵΃ѺѺ • goto ͢΀͚᥺承΅㷗͚ͭ͡Οͩ΢΅๋ṛͽͯ΃

    (e.g. parser Ψ 䨗ͥ䦒΀Ϳ)
  82. Struct ΁ͺ͚ͼ • ᥺ΥͰ;Ꭳ΢͵䯤᭜֛ • Struct ΁͵͚ͭͼ̵ϮϊϐϖΨኞΚͯͩ;͢ͽͣΡ΄ͽͩ΢͢ ΀͚;তΔΟ΀͚ • Perl

    ΄ηϣυδμϕ͹Γ͚Θ΄ • 抍กͭڊͯ;奰ΥΟ΀͚΄ͽ托奞΅፜ͣΔͯ
  83. interface ΁ͺ͚ͼ • Interface ΁΅伋͵ͯΏͣ樛හ΄ਧ嬝Ψ䨗ͣΔͯ • go ΄ interface ΅

    Java ;晅͹ͼกᐏጱ΁μ϶φҁGo ͽ΅ Struct҂΁奄ͻͣΔͱΩ • Struct ΄ො΁ interface ͢伋͵ͯΏͣϮϊϐϖΨͯΏͼ䋚ᤰͭ͵;ͣ΁̵interfcae Ψ䋚ᤰͭ͵;͚͜ᇫ丆΁΀ΠΔͯ • Interface ΁͘ΔΠ΁Θग़ͥ΄ਧ嬝Ψ䨗͚ͼͭΔ͜;䋚ᤰͯΡ㯎͢य़䄜΀͵Η̵ग़ ͥ΄䁰ݳ interface ΁΅1뺶හ㮆ᑕଶͭ͡䨗͡΢ΔͱΩ • 抍กͭڊͯ;奰ΥΟ΀͚΄ͽ托奞΅፜ͣΔͯ
  84. code formatter ΁ͺ͚ͼ (1) • go ΅ϔϢζϸϕͽ go fmt ;͚͜πϫЀϖ͘͢Ρ΄ͽͩ΢Ψֵ

    ͩ͜;ͽπЄϖΨෆ୵ͯΡͩ;͢ͽͣΡ • go ͽ΅ go fmt Ψ᭗ͯͩ;͢୮͵Πڹ΁΀͹ͼ͚Ρ͵Η̵πЄ ϔΰЀνφόαϸ΅قӮኴو᭗ • perl ͽ΅ perltidy Κ App::PRT ΀Ϳ͘͢ΠΔͯ΃Ѻ
  85. code formatter ΁ͺ͚ͼ (2) • perl ΄䁰ݳ΅̵parser ΅ C ͽ䨗͡΢ͼ͠Π͡ͺ㵕ጱ΁

    AST Ψ 䯤塈ͯΡ͵Ηᶉጱᥴຉͽ΅ᴴኴ͘͢͹͵ΠͭΔ̵ͯ͢go ͽ΅ ͳΘͳΘπЀϞαϸ᥺承ͽ͘Ρ͵Ηͩ΄旽Π΅䜷͚Κͯͥ΀ͼ ͚Ρ • perl ͽΘ PPI Κ Compiler::LexerΚ Compiler::Parser Ψֵ͞Ά ͡΀Π΄ϹϦϸͽᶉጱᥴຉ΅ݢᚆͽͯ͢ਠኃ΁ΚΡ΄΅΀͡΀ ͡櫞͚ͭͽͯ΃
  86. code formatter ΁ͺ͚ͼ (3) • go fmt ΨΘ;΁ͭ͵ go import

    Κ go returns ΀Ϳ΄ϑЄϸΘ ͘Ρ • च๜ጱ΁΅πЄϖ᮱ړ΄ෆ୵΅Ӟ姼ͽ̵import ΄ෆ୵ϸЄϸ ͢ͷΝ͹;晅͹͵Π return ෈Ψ愆ਠͭͼͥ΢͵ΠͯΡ • य़ͣͥ䄜ΥΟ΀͚΄ͽϤϺυδμϕ㶨֖ͽֵ͜Θ΄Ψࢴਧͯ ΢Ά㺔氂΀ͭ
  87. linter ΁ͺ͚ͼ • go ΅ᶉጱᥴຉ͢ go ᛔ֛ͽΚΠΚ͚ͯ͵Η̵linter Θ꧌䋚ͭͼ͚Δͯ • 䰤伛πϫЀϖ;ͭͼ͘Ρ

    go vet ΚςЄϖϞЄϓΰ΄ go lint ΅๋֗ᴴֵ͜;͚͚ͽ ͭΝ͜ • go vet ΅πЀϞαϸε϶Є΁΅΀Ο΀͚ͧͿϮϯϷϷЄμ΀ͿͫΔͬΔ΀㺔氂΁΀ Πͳ͜΀πЄϖΨර͞ͼͥ΢Δͯ • go lint ΅ΞΠΞ͚πЄϔΰЀνφόαϸΨ suggest ͭͼͥ΢Δͯ • ͫΟ΁ͣΗ奞͡΀ώδϐμΨᤈ͚͵͚䁰ݳ΅ https://github.com/dominikh/go-tools Ψ手ͯͩ;ΘͽͣΔͯ
  88. formatter Θ linter Θᛔړͽ͚ͷ͚ͷπϫ ЀϖΨݜͥ΄ͽ΅΀ͥ Editer ΁戔ਧͯΡͩ ;͢਻ฃ΀΄ͽͳ΄Ξ͜΁ͭΔͭΝ͜

  89. ΅͚

  90. ίυδЀύ • ΀Ͳ Go ͡ • Perl Monger ͢ Golang

    ΨতΗΞ͜͹ͼ䦒΁Ϝϫ͹͵;ͩΣ • Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • Web API Ψ֢͹ͼΕΞ͜ • ϓφϕΨ䨗ͥ • Docker Ϡϸϖͭͼ㵕ͯ͡ • Δ;Η
  91. Go ͽ๋ṛ΄ Web API Ψ֢͹ͼΕΞ͜Ѻ • ίυδЀύ • WebςЄϝЄΨᒈͼΡ •

    πЄϖ΄䄜ๅ͘͢͹͵Οܨଷ΁ϠϸϖͯΡ • Database ΁ίμψφͯΡ • Web API Ψ䨗͚ͼ͚ͥ
  92. WebςЄϝЄΨᒈͼΡ (1) • net/http Ψֵͩ͜;ͽ墋㶨΁ Web ςЄϝЄΨᒈͼΡͩ;͢ͽͣΡ • net/http ΅ςЄϝЄ΁Θμ϶αίЀϕ΁Θ΀Ρṛ䱛ᚆ΀϶αϣ϶ϷЄ

    • ςЄϝЄ΄䁰ݳ΅ٖ᮱ͽ㵛ಋ΁ gorutine Ψኞ౮ͭͼϷμεφϕ͢奰Υ͹͵ ΟᏈ䬤ͭͼͥ΢Ρ΄ͽ΀΁Θᘍ͞Ͱ΁ϷϊЄφΨ㵁ሲጱ΁ֵͩ͜;͢ͽͣΡ • Graceful ShutdownΘ墋㶨΁䋚匍ͽͣΡ΄ͽΞ͚ • HTTP/2 ΁Θ䌏䖕ͭͼ͚Ρ
  93. ͩͩ΁πЄϖΨ䨗ͥ

  94. WebςЄϝЄΨᒈͼͼΕΡ (2) • middleware ΁䌏䖕ͭͼΕΡ • Plack ΄ midleware ;ΑΒݶ༷ͮஷ΄Θ΄΀΄ͽ

    Plack Κ͹ ͼ΢Ά䮭㵛ͽͯ • ͚ͺΘ΄όϫϚλ΄ኮ؟Ψ毣΁௏͚ၶ͡ΏͼͥͶ͚ͫ
  95. ͩͩ΁πЄϖΨ䨗ͥ

  96. WebςЄϝЄΨᒈͼͼΕΡ (3) • middleware Ψ䨗ͥ;ͣ΄ဳ఺ᅩ • http.responseWriter.WriteHeader() ͭ͵͘;΅ Header Ψ᭄ے

    ͯΡͩ;͢ͽͣ΀͚΄ͽ䶲ΨͺͧΡ஠ᥝ͘͢Ρ • ͵;͞Ά Content-Length Κ X-Frame-Open ΀Ϳ΄ Header Ψ middleware ΄஍㳌ቘͽ᭄ےͯΡͩ;͢ࢯ櫞 • Ε΀ͫΩͿͭ͜ͼΔͯ͡ҘҘ
  97. Web ςЄϝЄΨᒈͼΡ (4) • Graceful Shutdown ΁䌏䖕ͯΡ • ςЄϝЄΨෛ͚ͭ gorutine

    ͽ᩸㵕ͭͼ̵main ͽ signal ϕ ϶ϐϤͯΡͩ;ͽ̵ྲ斃ጱ墋㶨΁䋚匍ͽͣΡ
  98. ͩͩ΁πЄϖΨ䨗ͥ

  99. net/http 墋㶨ͽͯ΃Ѻ

  100. 䄜ๅ͘͢͹͵Οܨଷ΁ϠϸϖͯΡ (1) • Ͷ͚͵̵͚ςЄϝЄΨ䨗͚ͼΡ䦒΅πЄϖΨ䨗ͣ䟵͞͵Οᛔ㵕 ጱ΁Ϡϸϖ͢ᩳ͹ͼٚ᩸㵕ͭͼΑ͚ͭͽͯ΃ • realize Ψֵ͜;墋㶨΁䋚匍ͽͣΔͯ • realize

    Ψ᩸㵕ͯΡ;ᛔ᫝΄ςЄϝЄΘᒈͷӤ͢͹ͼϢήαϸ ΄䄜ๅΚίμψφϺν͢憎Ο΢ͼ䮭͚ͭͽͯ΄ͽͲΈֵ͚Δ ͭΝ͜
  101. 䄜ๅ͘͢͹͵Οܨଷ΁ϠϸϖͯΡ (2) • go get github.com/oxequa/realize ͭͼ realize start ͯΡͶͧ

    ͽ *.go Ϣήαϸ΄哶憙Ψͭͼͥ΢Δͯ • 戔ਧϢήαϸ΅ڡࢧ᩸㵕䦒΁ .realize.yaml ΁ͺͥΟ΢Ρ΄ͽ̵ ͘;΅奞ͥ͡抠ෆͭͼ͚ͣΔͭΝ͜
  102. Database ΁ίμψφͯΡ (1) • database/sql ΄αЀόЄϢδЄφΨ䋚ᤰͭ͵ driver Ψֵͩ͜; ͽ Database

    ΁ίμψφͽͣΔͯ • Perl ΄ DBI ΄Ξ͜΀Θ΄ͽϔϢήμϕ • Քࢧ΅ github.com/go-sql-driver/mysql Ψֵ͚Δͯ
  103. Database ΁ίμψφͯΡ (2) • go-sql-driver-mysql ΅ go ͽقͼ䨗͡΢ͼ͠Π̵libmysqlclient ΁ׁਂͭͼ͚΀͚ •

    ͩ΢΁ΞΠಅ̸ libmysqlclient Ψֵ͹ͼ͚Ρग़ͥ΄᥺承;䞉 㵕͢吖΀Ρ;ͩΣ͘͢ΠΔͯ
  104. Database ΁ίμψφͯΡ (3) • database/sql ΅πϚμτϴЀϤЄϷЀνΨᤈ͚Δͯ • ͜Δͥͺ͡͞ΆϞϢζЄϫЀφ͢ڊΡӞොͽϜϫΠͿͩΣΘ͘ Ρ

  105. Database ΁ίμψφͯΡ (4) • πϚμτϴЀΨ揳ΠͯͤͼͭΔ͜㺔氂 • Ϸμεφϕ͢ϝЄφϕͭ͵;ͣ΀Ϳͽ go ΄ concurrency

    ͢䃀य़ͭͼӞ 䶲΁ mysql ΁πϚμτϴЀΨ揳͹ͼͭΔ͚ max_connection ΨηЄϝЄ ͭ͵ΠϷϊЄφΨ͚ᷣͺΌͭͼͭΔ͜ݢᚆ௔͘͢Ρ • Δ͵̵僻洏΁ idel ΄ള姆͢ྜ͹ͼͭΔ͜ • DB.SetMaxIdleConns() ; DB.SetMaxOpenConns() ͽګகͯΡͩ;͢ ͽͣΡͩ͢΄㮔΁΀΁Ψف΢΢Ά͚͚͡΅ίϤϷξЄτϴЀ΄ᥝկེᒫ
  106. Database ΁ίμψφͯΡ (5) • wait_timeout ΁୚͹͡͡Ρڹ΁ίϤϷξЄτϴЀ㯎͡ΟπϚμ τϴЀΨᏈ䬤ͭ͵͚ • DB.SetConnMaxLifetime() ͽګகݢᚆ

    • wait_timeout ͭ͵Π֜Ο͡΄ቘኧͽ MySQL ͡ΟπϚμτϴЀ ͢ڔෙͫ΢͵䁰ݳ΅ίϤϷξЄτϴЀ͢ϷμεφϕΨᤈ͹ͼڡ Ηͼړ͡Ρ΄ͽͩͩ΄϶γЀϖϕϷϐϤΨಪ͞Ρ΄΁ํ㵁
  107. Database ΁ίμψφͯΡ (6) • Query() Κ Exec() ͽϤϹЄφϨϸύЄΨֵ͹͵䁰ݳ̵Prepare Statement ͢咲ᤈͫ΢ͼͭΔ͜

    • Prepare ΅πϚμτϴЀ㶨֖ͽٖ᮱ጱ΁κϰϐτϲͫ΢Ρ • ͭͭ͡ग़ͥ΄ WebίϤϷξЄτϴЀͽ΅䯭̸΀μεϷЄ͢咲ᤈͫ΢Ρ΄ͽϟϐϕሲ΅͡΀Π͚֗ • Prepare Ψ咲ᤈͱͰ΁ϤϹЄφϨϸύЄΨ go ͽᗝ䟵ͯΡ΄͢অΔ͚ͭ • fmt.Sprintf() Ψֵ͜ොဩ͢ϖκϲϮЀϕͫ΢ͼ͚Ρ̵͢SQL Injection ͢ோ͚ • MySQL ΄䁰ݳ̵Open䦒΄ params ΁ interpolateParams=true ΨͺͧΡͩ;ͽ driver ΄Ӿͽᗝ䟵 Ψͭͼͥ΢Ρ΄ͽμεϷЄ΄咲ᤈහ͢仂Ρ • 托ͭͥ΅ http://dsas.blog.klab.org/archives/52191467.html
  108. Database ΁ίμψφͯΡ (7) • 䋚檭΁ϔЄόΨ SELECT ͭͼΕΔͭΝ͜ • 1ϹπЄϖͶͧݐ஑ͭ͵͚䁰ݳ΅ DB.QueryRow(…).Scan(…)

    Ψ ͺ͚͡Δͯ • Scan() ΁΅ϪαЀόЄΨჁͯ஠ᥝ͘͢ΠΔͯ • go ΄୚හ΅㮔Ⴡͭ΀͵Η
  109. None
  110. Database ΁ίμψφͯΡ (8) • 愢හᤈݐ஑ͯΡ䦒΅ DB.Query(…) Ψֵ͚Δͯ • 䜟Π㮔΅αϓϹЄόЄ΁΀͹ͼ͚ͼ̵ͳ΢ʹ΢Ψ Scan()

    ͭͼ ͚ͥ୵ • ε϶Є͢咲ኞͯΡ堞ಅ͢ग̵़ͥ奾䯤儴櫏΁΀ΠΔͯ
  111. None
  112. Database ΁ίμψφͯΡ (9) • ϕ϶ЀσμτϴЀΨڥአͭͼ INSERT ͯΡ • ϕ϶ЀσμτϴЀΨڥአͯΡ䁰ݳ΅ DB.Begin()

    Ψ咲ᤈͭͼ Tx Ψݑͧݐ Ρ • ͩ΄ Tx Ψֵ͹ͼ Tx.Exec() Ψᤈͩ͜;ͽϕ϶ЀσμτϴЀ㳌ቘ΁΀Ρ • Tx ͽ΅΀ͥ DB.Exec() ͭ͵䁰ݳ΅ϕ϶ЀσμτϴЀ㳌ቘ΁΀Ο΀͚΄ ͽဳ఺
  113. None
  114. ͚Σ͚Σ䨗͚ͼͣ͵ͧͿ ኞͽΚΡ΄奾䯤ͣͺ͚

  115. database/sql ΄϶ϐϞЄΨֵ͜ • github.com/jmoiron/sqlx Ψֵ͜;̵DB.Get() Κ DB.Select() ͽ Struct ΁ϫϐϡЀνͭͼͥ΢Ρ̵ͭrows

    ΄ Close() ΀ͿΘ晒ڔ΁ ᤈ͹ͼͥ΢Ρ • Struct ΁΅όνΨͺ͡͹ͼ `db:”column”` ΀Ϳ;䨗͚ͼ͚ͧΆ䌏䖕 ͭͼͥ΢Ρ • sqlx ΅ database/sql ΄ͯΏͼ΄ل樄樛හΨ䋚ᤰͭͼ͚Ρ΄ͽ̵ QueryRow() ΀ͿΘͳ΄ΔΔֵͩ͜;͢ͽͣΡ
  116. None
  117. None
  118. ͡΀ΠφϐκϷͭΔͭ͵΃ѺѺ

  119. ϕ϶ЀσμτϴЀ㳌ቘΨ䮭΁ͯΡ • github.com/Code-Hex/sqlx-transactionmanager Ψֵ͜;ϕ϶ЀσμτϴЀ 㳌ቘ͢䮭΁΀ΠΔͯ • Perl ΄ DBIx::TransactionManager ΄Ξ͜΀Θ΄

    • Ϛφϕͭ͵ϕ϶ЀσμτϴЀΨ墋㶨΁䨗ͥͩ;͢ͽͣΡ • sqlx΄϶ϐϞЄ΁΀͹ͼ͚Ρ΄ͽͯΏͼ΄樛හ͢ڥአݢᚆ • ֵ͜䦒΅ tm ϞϐξЄυ΄ϕ϶ЀσμτϴЀϣϺϐμ΄ΕΨڥአͭ͵ො͢Ϝϫ Π΁͚ͥ
  120. None
  121. Ͷ͚Όͯ͹ͣΠѺ

  122. μεϷЄϠϸύЄΨֵ͜ • Κ͹·Π IN ܄Ψᛔێͽ䨗ͥ΄΅ᬔ͚Θ΄͘͢Ρ΄ͽμεϷϠϸύЄΨ ֵ͚ΔͭΝ͜ • ᛔړ΅ github.com/Masterminds/squirrel Ψֵ͹ͼ͚Δͯ

    • JOIN ΨތΖͶ͚͵͚΄ξЄφ΄μεϷЄ͢ኞ౮ͽͣΡ΄ͽ׎ڥ • Perl ΄ SQL::Maker ͹Γֵ͚͚ͪͩͷ • لୗϖκϲϮЀϕ΁ςЀϤϸ͢掓੄΀΄ͽֵ͚ො΅ͳͷΟ݇ᆙ΄ͩ;
  123. ͿΩ΀μεϷЄ͢咲ᤈͫ΢ͼ͚Ρ͡ᎣΡ • ίϤϷξЄτϴЀ͢य़ͣͥ΀͹ͼͥΡ;Ԩ๗ͱ΂μεϷЄ͢咲ኞͯΡͩ;͢Δ Δ͘ΠΔͯ • ͳΩ΀;ͣ΅咲ᤈͫ΢ͼ͚ΡμεϷЄΨԃ᥃΁ͭ͵͚ͽͯ΃ • tcpdump Ψ;΢ΆΥ͡ΡͧͿ樄咲Ӿ΅櫏΁Ϻν΁ڊͭ͵͚ •

    github.com/gchaincl/sqlhooks Ψֵ͞Ά墋㶨΁μεϷЄΨϺλЀνͽͣΔͯ • perl ͽ͚͜ Devel::KTYProf Κ DBI:x::QueryLog ΄Ξ͜΀Θ΄
  124. None
  125. ͫͼ

  126. ͩͩΔͽͣ͵Ο Web API Ψ ͪΠ͹;ͧ͡ͳ͜ͽͯ΃ѺѺ

  127. Web API Ψ䨗͚ͼ͚ͥ (1) • RESTful ͹Γ͚ JSON Ψᬬͯ API

    Ψ䨗͚ͼ͚ͥ • Twitter͹Γ͚ͷΝ͹;ͭ͵ϮϐψЄυΨಭᑤͽͣΡ API Ψ戔懯ͭΔͯ • POST /users • GET /users/{user_id} • POST /messages • GET /messages/{message_id} • Ε͵͚΀ఽͮ
  128. Web API Ψ䨗͚ͼ͚ͥ (1) • ᇙਧ΄ HTTP Method ͡ͺᇙਧ΄ path

    զक़Ύ΄ίμψφ΅ε϶Є΁ͭ ͵͚ • {user_id} ΀ͿΨ͚͚ఽͮ΁઀樄ͭ͵͚ • ;௏͚Δͯ΃ • net/http ኞͽӤ懿Ψ伋͵ͯ΄΅΀͡΀͡ᬔ͚ • HTTPϸЄόЄ΄ github.com/gorilla/mux Ψֵ͚ΔͭΝ͜
  129. None
  130. gollila/mux Ψֵ͞ΆͶ͚Ό φϐκϷͧ͡Δͯ΃

  131. negroni Ψֵ͜ • net/http ኞͽ΅ HandlerFunc() ͪ;΁ middleware Ψ晒አͭͼ ͚͡΀ͥͼ΅΀ΟͰय़䄜ΗΩͿ͜΀΄ͽ

    negroni Ψֵ͚Δͯ • negroni Ψֵͩ͜;ͽ̵ᇙਧ΄ path ΁䌏ͭͼͶͧ middleware Ψ晒አͭ͵Π̵愢හ΄ middleware Ψق֛΁晒አͯΡͩ;͢ φϐκϷ;ͧ͡ΡΞ͜΁΀ΠΔͯ
  132. Web API ΄䯤౮ (1) • Handler -> Service -> Repository

    ;͚͜Ξ͜΁㳌ቘ͢ၞ΢ΡΞ ͜΁䋚ᤰͭΔͯ • ݱ̸΅ interface ΨՕͭ͵ΚΠݐΠΨᤈ͜ • ͚ͩ͜͜䯤౮Ψ;Ρͩ;ͽݱ䍅ͽ΄䱛ᚆ͢ړ櫝ͫ΢ϓφϕΘ䨗 ͣΚͯͥ΀ΠΔͯ
  133. Web API ΄䯤౮ (2) • Handler • HTTP Request Ψݑͧ՞ͧͼϞ϶ϮЄό΄㳌ቘΨᤈ͚

    Service Ψ޷Ήڊͯ • Service ΁ HTTP 樛昧΄Θ΄΅Ⴡͫ΀͚
  134. Web API ΄䯤౮ (3) • Service • ϠυϚφϺυϐμΨ䨗ͥ;ͩΣ • ͳ΄ݷ΄᭗ΠςЄϠφ΄ᙋ

  135. Web API ΄䯤౮ (4) • Repository • ϔЄόϦЄφΚͳ΄՜΄φϕϹЄυΎ΄ίμψφΨ檱ᠰͯΡ Θ΄ •

    愢櫏΅Ϻυϐμ΀Ϳ΅䨗͡΀͚΄͢ቘమ • sqlx.DB ΅ repository ΄ Struct ΄Ӿ΁೮͵ͱΡΞ͜΁ͯΡ
  136. None
  137. None
  138. None
  139. ίυδЀύ • ΀Ͳ Go ͡ • Perl Monger ͢ Golang

    ΨতΗΞ͜͹ͼ䦒΁Ϝϫ͹͵;ͩΣ • Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • Web API Ψ֢͹ͼΕΞ͜ • ϓφϕΨ䨗ͥ • Docker Ϡϸϖͭͼ㵕ͯ͡ • Δ;Η
  140. ϓφϕΨ䨗ͥ (1) • ϓφϕ΄䨗ͣො • ϓφϕ΅ *_test.go ;͚͜͜ݷڹͽ΄Ϣήαϸ΁䨗ͥ • ֺ͞Ά̵server.go

    Ͷ͹͵Ο server_test.go • testing ϶αϣ϶ϷΨ import ͭͼ func TestXXX(t *testing.T) ͡ΟতΔΡ樛හΨ䨗ͥ
  141. ϓφϕΨ䨗ͥ (2) • ϓφϕ΄䋚ᤈ΄՛ො • go test ͽ䋚ᤈͽͣΡ • go

    test ./… ͽٚ䒐ጱ΁ϓφϕͭͼͥ΢Ρ • Perl ΄ prove ΄Ξ͜΀Θ΄
  142. Service ΄ϓφϕΨᘍ͞Ρ (1) • ΔͰ Service ΄ϓφϕΨ䨗ͥͩ;Ψᘍ͞Δͯ • Repository ΅

    interface Ψ伋͵ͭͼ͚΢Ά͚͚΄ͽ̵ϓφϕӾ ͽ Struct Ψਧ嬝ͭͼͨ͘΢Ά૧๊ͭ͞Ρͩ;͢ͽ̵ͣService Ͷͧ΁ᵞӾͯΡͩ;͢ͽͣΔͯ
  143. Service ΄ϓφϕΨᘍ͞Ρ (2) • ducktyping ͶͧͶ; Service ΄ӾͽͷΙΩ;๗இͭ͵ Repository ΄Ϯ

    ϊϐϖ͢޷Ά΢ͼ͚Ρ͡Υ͡Ο΀͚͡Θͭ΢ΔͱΩ • gomock Ψֵ͜;̵樛හ΄޷Ήڊͭ΄殼ଧΚ޷Ήڊͭࢧහ΀ͿΨϓφϕ ͯΡͩ;͢ͽͣΔͯ • gomock ΅ mockgen ;͚͜πϫЀϖͿψϐϕͽڥአͭΔͯ • mockgen ΁ϯϐμͭ͵͚ go ϢήαϸΨჁͯ; interface Ψ伋͵ͭ͵ go ϢήαϸΨ generate ͭͼͥ΢Δͯ
  144. MySQL Ψֵ͹͵ϓφϕ (1) • github.com/lestrrat-go/test-mysqld Ψͺ͜͡ • ΕΩ΀य़অͣ Test::mysqld ΄

    go ϪЄϓΰЀν ͽͯ΃ • test ΄͵Ή΁ਠق΁μϷЄЀ΀ MySQL ΨᒈͷӤͨͼͥ΢ΡΚ ͺ • ΘͷΣΩ MySQL ΄ᒈͷӤͨ΁䦒樌͢͡͡Ρ΄΅᭿ͧΟ΢΀͚
  145. MySQL Ψֵ͹͵ϓφϕ (2) • ϓφϕΨṛ᭛΁ࢧͭ͵͚䁰ݳ΅Ԫڹ΁愢හ΄ MySQL Ψ Docker ΀Π MySQL::Sandbox

    ΀ΠͽᒈͷӤͨͼ͚͠ͼࢧͯ΄͢ፘ䄜ΥΟͰ͚͚䶲ͭ͢Δ ͯ • Perl Ͷ;ව̸ͩ΄͘͵Π΅ΚΠੱͥͫ΢ͼ͚ͼᛔړΘค Test::mysqld Ψ࿞ 姆۸ͯΡොဩΚApp::Prove::Plugin::MySQLParallel Ψ䨗͚͵ΠͭͼΔͭ͵ • Test::mysqld::pool ;͡ App::Prove::Plugin::MySQLPool ;͚͡Σ͚Σ͘Ρ • Go ͽ͚͚ఽͮ΁ΚΡොဩ΅Θ͜ͷΝ͚ᎸᑪͭΔͯ
  146. WebςЄϝЄ΄ϓφϕ • net/http ΁΅ httptest ;͚͜ WebςЄϝЄϓφϕአ΄϶αϣ϶ ϷЄ͘͢͹ͼͩ΢Ψͺ͜͡;墋㶨΁ϓφϕΨ䨗ͥͩ;͢ͽͣΔͯ • httptest.Server

    Ψֵ͜;䋚檭΁ςЄϝЄΨᒈͷӤͨͼͥ΢Ρ • net.Listen(“tcp”, “127.0.0.1:0”) ;ͥ͡;̵ᑮ͚ͼ͚ΡportΨ Listen ͭͼͥ΢Δͯ • Test::TCP ጱ΀䱛ᚆ͘͢͹ͼ׎ڥͽͯ΃
  147. ΅͚

  148. ίυδЀύ • ΀Ͳ Go ͡ • Perl Monger ͢ Golang

    ΨতΗΞ͜͹ͼ䦒΁Ϝϫ͹͵;ͩΣ • Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • Web API Ψ֢͹ͼΕΞ͜ • ϓφϕΨ䨗ͥ • Docker Ϡϸϖͭͼ㵕ͯ͡ • Δ;Η
  149. DockerͽϠϸϖͭͼ㵕ͯ͡ (2) • φϓϐϤ;ͭͼ΅ • golang:latest 厏हͽ go Ψ Linux

    ݻͧ΁Ϡϸϖ • alpine / busybox / scratch ΀Ϳ΄敽ᰁ΀αϮЄυ΁Ϡϸϖͭ ͵ϝαϗϷΨ COPY • ;͚͜ၞ΢ͽͯ
  150. DockerͽϠϸϖͭͼ㵕ͯ͡ (2) • net/http ϞϐξЄυΨڥአͭͼ͚Ρ;̵dynamic link ΁΀͹ͼͭΔ̵͚ϝα ϗϷΨπϡЄͭ͵Ͷͧͽ΅㵕͡΀͚ • `go

    build -a -tags netgo -installsuffix netgo` ΀Ϳ;ͭͼϠϸϖͯΡͩ;ͽ static link ΁΀͹ͼͥ΢Δͯ • ׁਂͭͼ͚Ρ϶αϣ϶Ϸͽ cgo Ψֵ͹ͼ͚Ρ䁰ݳ΅ͳ΢Θ dynamik link ΁ ΀Ρ΄ͽզӥ΄Ξ͜΁ͭΔͯ • `go build -a -tags netgo -installsuffix netgo --ldflags '-extldflags "-static"'`
  151. DockerͽϠϸϖͭͼ㵕ͯ͡ (3) • WebςЄϝЄ΅ net/http Ψֵͩ͜;͢Α;ΩͿ΀΄ͽ dynamic link ΁΀͹ͼͭΔ͜΄΅ϜϫΠ͢ͷ •

    ̵ͭͭ͡golang:latest ͽͳ΄ΔΔ docker build ͭͼͭΔ͜; πЀϓϗ͢ 700MB ᑕଶ΁΀͹ͼͭΔ͜΄ͽ̵හMB͡Οහ܈ MBᑕଶ΄ੜͫ΀ Linux ΨڥአͯΡͩ;Ψ͠㵣ΗͭΔͯ
  152. ΅͚

  153. ίυδЀύ • ΀Ͳ Go ͡ • Perl Monger ͢ Golang

    ΨতΗΞ͜͹ͼ䦒΁Ϝϫ͹͵;ͩΣ • Go ΄᥺承՛䯭ͽ਍Ό Perl ;΄ፘ晅ᅩ • Web API Ψ֢͹ͼΕΞ͜ • ϓφϕΨ䨗ͥ • Docker Ϡϸϖͭͼ㵕ͯ͡ • Δ;Η
  154. Δ;Η • Perl ͽङ͹͵Ꭳ挷Ψͺ͡͹ͼ Go ͽ WebςЄϝЄΨ䨗͚ͼ͚ͥ ොဩ΁ͺ͚ͼ扖ͭΔͭ͵ • ᛔړᛔ᫝ΔͶ娞ଶ͚֗͢΄ͽ୚ͣ姆ͣ毇䔴͹ͼ͚ͣ͵͚ͽͯ

    • Perl ֵ͹ͼ͚͵Ո͢奾䯤 Go Ψֵ͹ͼ͚Ρ΄ͽ̵Ϳͩ͡ͽΕ͵ Ξ͜΀϶αϣ϶Ϸ͘͢͹͵Πͭͼ΀ΩͶ͚͚͡ͽͯ΃
  155. ͭ͠Δ͚