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

Goで作るWebアプリケーション Echo 編 といいつつGoの説明多い

Goで作るWebアプリケーション Echo 編 といいつつGoの説明多い

Takashi Funato

August 07, 2019
Tweet

More Decks by Takashi Funato

Other Decks in Programming

Transcript

  1. { Simplicity. ࣗݾ঺հ • ᢠށɹོ(tfunato) • αʔόαΠυҰے • PHPɺNode.jsɺJavaɺetc •

    AWS ೝఆσϕϩούʔ ΞιγΤΠτ • ༗ޮظݶ੾Εͨ • ͳΜͰ΋΍ͬͯΔ • ୭͔কعΛࢦͦ͏ • ࡉ͔͍಺༰͸ϓϩϑΟʔϧΈͯͪΐ 2
  2. Goͷઆ໌ - ಛ௃ - • ੩తܕ෇͚ • ༷ʑͳܕ • ߏ଄ମͱϙΠϯλ

    • ΦϒδΣΫτࢦ޲Ͱͳ͍ • ݴޠ͕࣋ͬͯΔϥΠϒϥϦ • ίϯύΠϧ • ಈ࡞؀ڥ • ϝϞϦ؅ཧ 5
  3. Goͷઆ໌ - ༷ʑͳܕ - جຊܕ ओͳ΋ͷ 7 ໊લ આ໌ ॳظ஋

    ਺஋ܕ JOU ࣮૷ܕʹґଘͨ͠ܕ  JOU ූ߸෇͖ɺఴࣈ͸CJU਺  JOU JOU JOU จࣈྻ TUSJOH จࣈྻΛ֨ೲ ۭจࣈ ਅِ஋ CPPM USVF GBMTF '"-4&
  4. Goͷઆ໌ - ༷ʑͳܕ - ෳ߹ܕ ओͳ΋ͷ 8 ྫ આ໌ ഑ྻܕ

    <>JOU ݸͷཁૉΛ࣋ͬͨJOU εϥΠεܕ <>JOU Մม௕഑ྻ ϙΠϯλܕ JOU ϝϞϦ্ͷΞυϨεΛද͢ܕ Ϛοϓܕ NBQ<TUSJOH>JOU TUSJOHΛLFZɺJOUΛWBMVFʹ΋ͭ ؔ਺ܕ GVOD JOU JOU ؔ਺Λ֨ೲ͢Δܕ ߏ଄ମܕ UZQFTUSVDU ߏ଄ମΛ֨ೲ͢Δܕ ΠϯλʔϑΣʔεܕ JOUFSGBDF\^ جຊతʹԿͰ΋֨ೲͰ͖Δܕ
  5. Goͷઆ໌ - ߏ଄ମͱϙΠϯλ - • ߏ଄ମ • ৽͘͠ܕΛ࡞Δ • ෳ߹σʔλू߹

    • ϑΟʔϧυͱ͍͏ • ϙΠϯλ • ϝϞϦͷΞυϨε • ؔ਺ͷҾ਺Ͱ໌ࣔ͠ͳ͍ͱɺجຊతʹ஋౉͠ 9
  6. Goͷઆ໌ - ߏ଄ମ - 10 // ߏ଄ମܕͷએݴ type hoge struct

    { id int name string } // ߏ଄ܕͷॳظԽ ϑΟʔϧυࢦఆ var h hoge h.id = 1 h.name = “test” // ߏ଄ܕͷॳظԽ ߏ଄ମϦςϥϧ h := hoge{ id : 1, name : “test”, }
  7. Goͷઆ໌ - ϙΠϯλ - 11 CݴޠͷϙΠϯλͱಉ͡Α͏ʹϝϞϦͷΞυϨεΛѻ͏ var a int =

    100 var aPointer *int = &a // &͸ΞυϨεԋࢉࢠɻΞυϨεΛऔಘ͢Δ͜ͱ͕Ͱ͖Δ // * ͸ϙΠϯλܕɻΞυϨεΛ֨ೲͰ͖Δ ஋౉͠ɺϙΠϯλ౉͠Λҙࣝ͢Δ ੑೳ΍ɺίʔυྔʹؔΘͬͯ͘Δ
  8. Goͷઆ໌ - ΦϒδΣΫτࢦ޲Ͱͳ͍ - 12 • ΦϒδΣΫτࢦ޲Ͱ͸ͳ͍͕ɺߏ଄ମʹࣅͨΑ͏ͳৼΔ෣͍ Λͤ͞Δ͜ͱ͕Ͱ͖Δ type Name

    struct { FirstName string LastName string } type Printer interface { print() string } func (n *Name) print() string { return n.FirstName + LastName } name := Name{FirstName: “Hoge”, LastName: “Fuga”} printName := name.print()
  9. Goͷઆ໌ - ίϯύΠϧ - • ίϯύΠϥݴޠͰ͢ • OS ωΠςΟϒόΠφϦͰಈ͖·͢ •

    ґଘ͢ΔϥΠϒϥϦͳ͠ • ֤OSຖʹίϯύΠϧ • ಈ࡞؀ڥɿPOSIXʢLinuxɺMacOS)ɺWindows • CݴޠͷLinkerͱ͔͍Βͳ͍ • go build • ϏϧυͰ͸MakefileΛΑ͘࢖͏ 14
  10. Goͷઆ໌ - ϝϞϦ؅ཧ - • ϝϞϦ؅ཧ͸GCʹجຊ͓·͔ͤ • ͱ͸͍͑ɺ࡞Γ͕ѱ͚ΓΌϦʔΫ͢Δ • Α͠ͳʹ΍ͬͯ͘ΕΔ

    • ௨ৗɺϝϞϦͷΞϩέʔγϣϯ͸ίετ͕ߴ͍ • ͦΕͰ΋Go͸݁ߏૣ͘ಈ͘ 15
  11. GoͱWeb • ΋ͱ΋ͱݴޠࣗମͷϥΠϒϥϦ͕खް͍  // HTTPαʔό (main.go) package main import

    ( "io" "log" "net/http" ) func main() { helloHandler := func(w http.ResponseWriter, req *http.Request) { io.WriteString(w, "Hello, world!\n") } http.HandleFunc("/", helloHandler) log.Println("Listening...") http.ListenAndServe(":3000", nil) // open 3000 port }
  12. • ΊͬͪΌͨ͘͞Μ͋Δ • gin • beego • iris • echo

    • ͦͷଞɺRESTʹಛԽͨ͠΋ͷ΍RoutingʹಛԽͨ͠΋ͷ WebΞϓϦέʔγϣϯϑϨʔϜϫʔΫ 
  13. EchoΛબΜͩΘ͚ • Node.JSͷExpressʹࣅ͍ͯΔ • ͲΕ΋ࣅͨΑ͏ͳײ͕͢͡Δ • GithubͷStar΋ΈΜͳɺ1ສ௒͑ͯΔ • υΩϡϝϯτͷྔɺ࣭͸Ͳ͏͔ʁ •

    GoogleͰώοτ͠΍͢͞ • αΠτͷ৘ใྔ • ։ൃऀͷ׆ൃ͞ • CommitɺϦϦʔεɺPRͷফԽͳͲ 24
  14. Echoͷઆ໌ • Echo ଎͍ʂͱ͋Δ • Ginͱൺ΂ͯ଎͍Β͍͠ • ଎͞͸ਖ਼ٛ • Data

    binding͕JSONɼXMLɼFormͳͲͪΌΜͱ͋Δ • RoutingΛGroupingͰ͖Δ • HTTPͷΤϥʔϋϯυϦϯά͠΍͍͢ • https://echo.labstack.com/ • 25
  15. Echoͷ঺հ - Routing - • جຊతͳRouting • /hello • /users/:id

    • /users/* • Grouping • adminGroup := e.Group(“/admin”) • adminGroupʹઃఆͨ͠΋ͷ͸ɺͦͷύεҎ߱Ͱ༗ޮʹͳΔ • “/admin/*” ͷύεͰ͸Basic ೝূ͍ͨ͠ͱ͔ • Method • GETɺPOSTɺPUTɺDELETE… 26
  16. Echoͷ঺հ - MiddleWare - • Echoࣗ਎Ͱ਺ଟ͘༻ҙ͞Ε͍ͯΔ • ΋ͪΖΜࣗ࡞Մೳ • BasicೝূɺgzipɺJWTɺCORSͳͲͳͲ

    • GroupϨϕϧͰ΋ɺRoutingϨϕϧͰ΋ • Group • admin := e.Group("/admin", middleware.BasicAuth()) • Route • e.GET("/", <Handler>, <Middleware…>) 27
  17. Echoͷ঺հ - RequestͱResponse - • Request͸ߏ଄ମͰఆٛͨ͠΋ͷʹBind͢Δ • ߏ଄ମͷλάͰ੍ޚ͢Δ 28 //

    RequestΛड͚औΔߏ଄ମܕͷએݴ type UserRequest struct { ID int `json:”id"` Name string `json:”name”` } ߏ଄ମͷλά KTPOCPEZ
 \ lJEz  lOBNFzlUFTUz ^ Λड͚Δઃఆ
  18. Echoͷ঺հ - RequestͱResponse - • JSONΛฦ͢ͷͰ͋Ε͹ɺResponse΋ߏ଄ମͰఆٛ • ߏ଄ମͷλάͰ੍ޚ͢Δ 29 //

    ResponseΛఆٛͨ͠ߏ଄ମܕͷએݴ type UserResponse struct { ID int `json:”id"` Name string `json:”name”` } ߏ଄ମͷλά KTPOCPEZ
 \ lJEz  lOBNFzlUFTUz ^ Λड͚Δઃఆ