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

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

17d4ef53b432ebf7c566fd6a11345570?s=128

yuuki takezawa

November 03, 2016
Tweet

Transcript

  1. 5.

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


    (औΓ૊Έத) • σʔλϕʔεࣗମͷ෼ׂ
 (ςʔϒϧͷ࠶ઃܭɺ໨తผʹ෼ׂɺNoSQLซ༻) • ԣஅతʹؔΘΔαʔϏεͱͷ࿈ܞ
 (message queue, ฒྻॲཧ)
  2. 7.

    class PointBalance { /** @var PointRepositoryInterface */ protected $repository; /**

    * @param PointRepositoryInterface $repository */ public function __construct(PointRepositoryInterface $repository) { $this->repository = $repository; } // snippet }
  3. 9.
  4. 11.

    { "id": 1598, "user_id": 123, "title": "ίϝϯτʹͨ͠Α", "content": "ϝʔϧ͕དྷͳ͍ΑͶ", "tags":

    [ { "id": 5098, "name": "ඒന" } ], "created_at": "2016-10-12 17:31:50" }
  5. 12.

    { "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" } } }
  6. 14.

    ϑϩϯτͰར༻͞ΕΔAPI • ϑϩϯτʹग़ྗ͞ΕΔ΋ͷ͚ͩͰ͸ͳ͘ɺ
 ͦͷޙΖʹ߇͑Δ΋ͷ • Ϣʔβʔొ࿥৘ใ • ϝʔϧૹ৴ • ͳʹ͔͠Βͷߦಈʹ൐͏Ճࢉݮࢉ

    • ඇಉظɺϦΞϧλΠϜ • τϥϯβΫγϣϯεΫϦϓτ • ฒྻϦΫΤετʹ଱͑͏Δ΋ͷ • ϑϩϯτ͔Β੾Γ཭͢͜ͱͰదࡐదॴͳݴޠ࠾༻
  7. 19.
  8. 21.
  9. 22.

    type Item struct { Language } type Language struct {

    Name string } Item{Language{"PHP"}}
  10. 23.

    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 }
  11. 25.

    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") }
  12. 28.

    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 }
  13. 29.

    defer ͱ panic • defer: ϝιου͕ऴྃ͢Δࡍʹίʔϧ͞ΕΔ • σʔλϕʔεͳͲͷ੾அͳͲॲཧͷ஗Ԇʹ࢖͏ • panic͸runtime

    errorʹ͋ͨΓɺ
 ࣮ߦ͕೉͍͠৔߹ʹͷΈར༻͢Δ • panic࣌ʹ΋defer͕ίʔϧ͞ΕΔ • ΤϥʔॲཧΛ͢ΔͷͰ͋Ε͹errorΛ໭͢ • panic͔Β͔ͳΒͣrecover()͢Δ
  14. 31.

    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 ""; } }
  15. 36.

    ಋೖલʹ࣮ફ • ϓϥΠϕʔτͰ։ൃ͍ͯͨ͠ΞϓϦͷAPIʹ·ͣಋೖ • ҆ఆՔಇͱ࣮૷͕ૣ͍ • OAuthαʔό࣮૷ • ߴෛՙͰ΋ඇৗʹ҆ఆ •

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