Go on App Engine

Ian Lewis
2011年 5月 26日

Google App Engine 上で Go 言語を使えるようになりました (Experimental!)。

Python はどうなの?

結局、スピードの問題かな?

アプリの作り方

Go の標準 http パッケージを使う

func init() {
  http.HandleFunc("/", func(w http.ResponseWriter,
                            request *http.Request)
  {
    c := appengine.NewContext(request)
    fmt.Fprintf(w, "Hello, world! from %s", c.AppID())
  })
}

データストアの読み込み

type Greeting struct {
    Body    string
    Date    datastore.Time
}
func init() {
  http.HandleFunc("/", func(w http.ResponseWriter,
                            request *http.Request) {
    c := appengine.NewContext(request)
    greetings := &[]Greeting{}
    datastore.NewQuery("Greeting").
              Order("-Date").
              GetAll(c, greetings)
    //...
  })
}

データストアの書き込み

type Greeting struct {
    Body    string
    Date    datastore.Time
}
func init() {
  http.HandleFunc("/save", func(w http.ResponseWriter,
                            request *http.Request) {
    c := appengine.NewContext(request)
    g := &Greeting{
      Body: request.FormValue("body"),
      Date: datastore.SecondsToTime(time.Seconds()),
    }
    datastore.Put(c, datastore.NewIncompleteKey("Greeting"), g)
    http.Redirect(w, request, "/", http.StatusFound)
  })
}