1

I have

http://localhost:8080/?key=ahFkZXZ-ZGV2LWVkdW5hdGlvbnIOCxIIVXNlckluZm8YLAw

I would like to ask on how to:

  1. Decode and convert the "key" to a *datastore.Key
  2. And use it to get an entity.

Thanks for your help!

4

1 回答 1

6

First: You should think about which packages you need this case. Since you're trying to read a GET value from a URL you need probably a function from net/http. In particular: FormValue(key string) returns GET and POST parameters.

Second: Now open the appengine/datastore documentation and find functions which do the following:

Now it's a really easy thing:

func home(w http.Response, r *http.Request) {
    c := appengine.NewContext(r)

    // Get the key from the URL
    keyURL := r.FormValue("key")

    // Decode the key
    key, err := datastore.DecodeKey(keyURL)
    if err != nil { // Couldn't decode the key
        // Do some error handling
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    // Get the key and load it into "data"
    var data Data
    err = datastore.Get(c, key, data)
    if err != nil { // Couldn't find the entity
        // Do some error handling
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}
于 2013-01-05T12:49:52.710 回答