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

phpアプリケーションをGo言語へ

 phpアプリケーションをGo言語へ

yuuki takezawa

November 03, 2016
Tweet

More Decks by yuuki takezawa

Other Decks in Programming

Transcript

  1. ໰୊ʹཱͪ޲͔͏ • σʔλϕʔεࢀরͷ੾Γ཭͠ • APIͷ੔උ json, xml, etc… • ΫϥΠΞϯτͷॲཧΛ؆ུԽ͢ΔҝʹɺHATEOAS΁


    (औΓ૊Έத) • σʔλϕʔεࣗମͷ෼ׂ
 (ςʔϒϧͷ࠶ઃܭɺ໨తผʹ෼ׂɺNoSQLซ༻) • ԣஅతʹؔΘΔαʔϏεͱͷ࿈ܞ
 (message queue, ฒྻॲཧ)
  2. class PointBalance { /** @var PointRepositoryInterface */ protected $repository; /**

    * @param PointRepositoryInterface $repository */ public function __construct(PointRepositoryInterface $repository) { $this->repository = $repository; } // snippet }
  3. { "id": 1598, "user_id": 123, "title": "ίϝϯτʹͨ͠Α", "content": "ϝʔϧ͕དྷͳ͍ΑͶ", "tags":

    [ { "id": 5098, "name": "ඒന" } ], "created_at": "2016-10-12 17:31:50" }
  4. { "id": 3746, "user_id": 123, "title": "ςετ", "content": "aaa", "tags":

    [ { "id": 6081, "name": "ΞϩϚΦΠϧ", "_links": { "self": { "href": "http://example.com/tags/6081", "method": "GET" } } } ], "created_at": "2016-10-14 16:57:37", "_links": { "self": { "href": "http://example.com/3746", "method": "GET" } } }
  5. ϑϩϯτͰར༻͞ΕΔAPI • ϑϩϯτʹग़ྗ͞ΕΔ΋ͷ͚ͩͰ͸ͳ͘ɺ
 ͦͷޙΖʹ߇͑Δ΋ͷ • Ϣʔβʔొ࿥৘ใ • ϝʔϧૹ৴ • ͳʹ͔͠Βͷߦಈʹ൐͏Ճࢉݮࢉ

    • ඇಉظɺϦΞϧλΠϜ • τϥϯβΫγϣϯεΫϦϓτ • ฒྻϦΫΤετʹ଱͑͏Δ΋ͷ • ϑϩϯτ͔Β੾Γ཭͢͜ͱͰదࡐదॴͳݴޠ࠾༻
  6. type Item struct { Language } type Language struct {

    Name string } Item{Language{"PHP"}}
  7. func handler(w http.ResponseWriter, r *http.Request) { person := &Person{"yuuki", "takezawa"}

    fullName(person) } func fullName(name UserName) { fmt.Println(name.Name()) } type Person struct { FirstName string LastName string } func (p *Person) Name() string { return p.FirstName + " " + p.LastName } type UserName interface { Name() string }
  8. func f(from string) { for i := 0; i <

    3; i++ { fmt.Println(from, ":", i) } } // ॱ൪͸อূ͞Ε·ͤΜ fun main() { go f("routine") go f("routine_one") go f(“routine_two") }
  9. func read(r io.Reader) <-chan message { lines := make(chan message)

    go func() { defer close(lines) scan := bufio.NewScanner(r) for scan.Scan() { lines <- message(scan.Bytes()) } }() return lines }
  10. defer ͱ panic • defer: ϝιου͕ऴྃ͢Δࡍʹίʔϧ͞ΕΔ • σʔλϕʔεͳͲͷ੾அͳͲॲཧͷ஗Ԇʹ࢖͏ • panic͸runtime

    errorʹ͋ͨΓɺ
 ࣮ߦ͕೉͍͠৔߹ʹͷΈར༻͢Δ • panic࣌ʹ΋defer͕ίʔϧ͞ΕΔ • ΤϥʔॲཧΛ͢ΔͷͰ͋Ε͹errorΛ໭͢ • panic͔Β͔ͳΒͣrecover()͢Δ
  11. upstream go_http { server 127.0.0.1:8080; server 127.0.0.1:8081; keepalive 300; }

    server { listen 80; server_name go.app; charset utf-8; access_log off; error_log /dev/null crit; location / { proxy_pass http://go_http; proxy_http_version 1.1; proxy_set_header Connection ""; } }
  12. ಋೖલʹ࣮ફ • ϓϥΠϕʔτͰ։ൃ͍ͯͨ͠ΞϓϦͷAPIʹ·ͣಋೖ • ҆ఆՔಇͱ࣮૷͕ૣ͍ • OAuthαʔό࣮૷ • ߴෛՙͰ΋ඇৗʹ҆ఆ •

    facebookgo/inject • ΦϒδΣΫτࢦ޲ϥΠΫʹ࣮૷͍͕ͯͨ͠ɺ
 Goͷ࡞๏ʹͷͬͨ΄͏͕γϯϓϧ • ϦϑϨΫγϣϯ͸ۃྗ࢖Θͳ͍