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

golang_hikaru.pdf

 golang_hikaru.pdf

Yosuke Furukawa

June 17, 2014
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. Golangྺɿ2ϲ݄͘Β͍ ࡞ͬͨ΋ͷ ! - socket.io-go-emitter - golang + angularjs Ͱ

    chat sample - qiita ΞυϕϯτΧϨϯμʔΛεΫϨΠϐϯά͢ Δπʔϧ
  2. net/httpͰWebαʔόΛͨͯΔ package main ! import ( "fmt" "net/http" "log" )

    ! func hello(w http.ResponseWriter, r *http.Request) { //fmt.FprintfͰwʹೖΔ΋ͷ͕ΫϥΠΞϯτʹग़ྗ͞ΕΔ fmt.Fprintf(w, "Hello World!") } ! func main() { // ΞΫηεͷϧʔςΟϯάΛઃఆ͢Δ http.HandleFunc("/", hello) // portΛࢦఆͯ͠ىಈ err := http.ListenAndServe(":9090", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
  3. net/httpͰWebαʔόΛͨͯΔ package main ! import ( "fmt" "net/http" "log" )

    ! func hello(w http.ResponseWriter, r *http.Request) { //fmt.FprintfͰwʹೖΔ΋ͷ͕ΫϥΠΞϯτʹग़ྗ͞ΕΔ fmt.Fprintf(w, "Hello World!") } ! func main() { // ΞΫηεͷϧʔςΟϯάΛઃఆ͢Δ http.HandleFunc("/", hello) // portΛࢦఆͯ͠ىಈ err := http.ListenAndServe(":9090", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } )BOEMF'VODͰSPVUJOH -JTUFO"OE4FSWFͰQPSUΛࢦఆͯ͠ىಈ
  4. ListenAndServeͷಈ͖ func (srv *Server) Serve(l net.Listener) error { defer l.Close()

    var tempDelay time.Duration // how long to sleep on accept failure for { rw, e := l.Accept() if e != nil { if ne, ok := e.(net.Error); ok && ne.Temporary() { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay) time.Sleep(tempDelay) continue } return e } tempDelay = 0 c, err := srv.newConn(rw) if err != nil { continue } go c.serve() } }
  5. ListenAndServeͷಈ͖ func (srv *Server) Serve(l net.Listener) error { defer l.Close()

    var tempDelay time.Duration // how long to sleep on accept failure for { rw, e := l.Accept() if e != nil { if ne, ok := e.(net.Error); ok && ne.Temporary() { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay) time.Sleep(tempDelay) continue } return e } tempDelay = 0 c, err := srv.newConn(rw) if err != nil { continue } go c.serve() } } ϦεφʔΛ௨ͯ͡ϦΫΤετΛड͚औΔ ৽͘͠ίωΫγϣϯͷJOTUBODFΛ࡞Δ HPSPVUJOFΛىಈ͠ɺϦΫΤετΛॲཧ Ϣʔβʔ͔ΒͷϦΫΤετ͸શͯ৽͍͠HPSPVUJOF ͰߦΘΕɺޓ͍ʹӨڹ͠ͳ͍࡞Γʹͳ͍ͬͯΔ
  6. RESTFul APIΛ࡞Δ package main ! import ( "fmt" "github.com/drone/routes" "net/http"

    ) ! // GET༻ͷHandler func getuser(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() id := params.Get(":id") fmt.Fprintf(w, "you are get user %s", id) } // PUT, DELETE, POST༻ͷ… ! func main() { mux := routes.New() // GET, PUT, DELETE, POST ༻ͷϋϯυϥʔؔ਺Λrouting͢Δ mux.Get("/users/:id", getuser) mux.Put("/users/:id", modifyuser) mux.Del("/users/:id", deleteuser) mux.Post("/users/", adduser) http.Handle("/", mux) http.ListenAndServe(":9090", nil) }
  7. RESTFul APIΛ࡞Δ package main ! import ( "fmt" "github.com/drone/routes" "net/http"

    ) ! // GET༻ͷHandler func getuser(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() id := params.Get(":id") fmt.Fprintf(w, "you are get user %s", id) } // PUT, DELETE, POST༻ͷ… ! func main() { mux := routes.New() // GET, PUT, DELETE, POST ༻ͷϋϯυϥʔؔ਺Λrouting͢Δ mux.Get("/users/:id", getuser) mux.Put("/users/:id", modifyuser) mux.Del("/users/:id", deleteuser) mux.Post("/users/", adduser) http.Handle("/", mux) http.ListenAndServe(":9090", nil) } ESPOFSPVUFTͱ͔HPXFCͱ͔ͷ ϧʔςΟϯά༻ͷ SENPEVMFTΛ࢖͏ େͨ͜͠ͱ͸΍ͬͯͳ͍ͷͰɺ OFUIUUQ͚ͩͰؤுΔࣄ΋Մೳ͕ͩɺ JE͔Β࢝·Δจࣈͷύʔεͱ NFUIPEʹΑΔSPVUJOHॲཧ͕໘౗ͳΒ ͜͏͍͏Ϟδϡʔϧ࢖͏ͷ΋͋Γɻ
  8. DBΞΫηε package main ! import ( "database/sql" _ "github.com/go-sql-driver/mysql" "log"

    ) ! func main() { ɹɹ// sql.Open ୈҰҾ਺ʹdriverͷछྨɺୈೋҾ਺ʹ઀ଓΞυϨεΛࢦఆɺ͜͜Ͱ͸hello ʹ઀ଓ͢Δ͜ͱΛએݴɻ db, err := sql.Open("mysql", "root@tcp(127.0.0.1:3306)/hello") // err͕͋Ε͹ͦΕΛදࣔ if err != nil { log.Fatal(err) } // deferͰؔ਺ͷऴΘΓʹͦΕΛclose defer db.Close() }
  9. DBΞΫηε package main ! import ( "database/sql" _ "github.com/go-sql-driver/mysql" "log"

    ) ! func main() { ɹɹ// sql.Open ୈҰҾ਺ʹdriverͷछྨɺୈೋҾ਺ʹ઀ଓΞυϨεΛࢦఆɺ͜͜Ͱ͸hello ʹ઀ଓ͢Δ͜ͱΛએݴɻ db, err := sql.Open("mysql", "root@tcp(127.0.0.1:3306)/hello") // err͕͋Ε͹ͦΕΛදࣔ if err != nil { log.Fatal(err) } // deferͰؔ਺ͷऴΘΓʹͦΕΛclose defer db.Close() } TRM0QFOͰ%#ΞΫηε։࢝
  10. SELECTૢ࡞ var ( id int name string ) // db.PrepareͰPreparedStatement࣮ߦ

    stmt, err := db.Prepare("select id, name from users where id = ?") if err != nil { log.Fatal(err) } // statementͷClose๨Εͣʹɻ defer stmt.Close() // stmt.QueryͰQueryΛ࣮ߦ rows, err := stmt.Query(1); for rows.Next() { err := rows.Scan(&id, &name) if err != nil { log.Fatal(err) } log.Println(id, name) } defer rows.Close();
  11. SELECTૢ࡞ var ( id int name string ) // db.PrepareͰPreparedStatement࣮ߦ

    stmt, err := db.Prepare("select id, name from users where id = ?") if err != nil { log.Fatal(err) } // statementͷClose๨Εͣʹɻ defer stmt.Close() // stmt.QueryͰQueryΛ࣮ߦ rows, err := stmt.Query(1); for rows.Next() { err := rows.Scan(&id, &name) if err != nil { log.Fatal(err) } log.Println(id, name) } defer rows.Close(); EC1SFQBSFͰ42-࣮ߦ͢Δ TUNU2VFSZͰ2VFSZΛ࣮ߦ ͜͜Ͱ͸ɺJEΛ࣮ߦ
  12. INSERTͱ͔UPDATEͱ͔DELETEͱ͔ ! stmt, err := db.Prepare("INSERT INTO users(id, name) VALUES(?,

    ?)") if err != nil { log.Fatal(err) } res, err := stmt.Exec(2, "haruyama") if err != nil { log.Fatal(err) }
  13. INSERTͱ͔UPDATEͱ͔DELETEͱ͔ ! stmt, err := db.Prepare("INSERT INTO users(id, name) VALUES(?,

    ?)") if err != nil { log.Fatal(err) } res, err := stmt.Exec(2, "gopher") if err != nil { log.Fatal(err) } TUNU&YFDͰߋ৽ܥͷ2VFSZΛൃߦ͢Δ
  14. ORM

  15. TL;DR OFUIUUQ NBSUJOJ CFFHP SFWFM GBMDPSF HPXFC  ESPOFSPVUFT HPSJMMBOFHSPOJ

    OFFETJNQMF 3&45'SBNFXPSL UPPMLJUDPNQBUJCMFXJUIOFUIUUQ 4JOBUSB&YQSFTTMJLF 'VMMTUBDL3P3MJLF
  16. martini ! • ϑϧελοΫϑϨʔϜϫʔΫ • martini͕sinatraͳΒͬͪ͜͸RoR, Play FrameworkɺͪͳΈʹॳظϖʔδ͘͢͝PlayͬΆ͍ • தʹmail

    sender΍websocket΋૊Έࠐ·ΕͯΔ • ϛυϧ΢ΣΞ΋ϑΟϧλʔͱ͍͏֓೦Λ࢖͏͜ͱͰ࢖ ͑Δ • ίʔυมߋΛݕ஌ͯࣗ͠ಈϦϩʔυ/ςετϑϨʔϜ ϫʔΫ͕தʹ͋ΔɺͳͲ։ൃʹ΋ศར
  17. martini ! ! • ͪ͜Β΋ਖ਼֬ʹ͸πʔϧΩοτ • router, secure session, websocket౳ɺ·͊WebΞϓϦ࡞

    Δʹ͸࢖͏ΑͶ͍ͬͯ͏ͷ͕ἧͬͯΔɻ • isucon ͍ͬͯ͏web ΞϓϦߴ଎Խνϡʔχϯάίϯςετ Ͱ࢖ΘΕͯΔɻ • ଞʹ΋࣮͸revelͷதͰ࢖ΘΕͯͨΓͱ൚༻ੑߴ͍ɻ !
  18. WAF·ͱΊ OFUIUUQ NBSUJOJ CFFHP SFWFM GBMDPSF HPXFC  ESPOFSPVUFT HPSJMMBOFHSPOJ

    OFFETJNQMF 3&45'SBNFXPSL UPPMLJUDPNQBUJCMFXJUIOFUIUUQ 4JOBUSB&YQSFTTMJLF 'VMMTUBDL3P3MJLF