sag*_*git 1 google-app-engine go google-cloud-datastore
我有
http://localhost:8080/?key=ahFkZXZ-ZGV2LWVkdW5hdGlvbnIOCxIIVXNlckluZm8YLAw
Run Code Online (Sandbox Code Playgroud)
我想询问如何:
*datastore.Key
谢谢你的帮助!
第一:你应该考虑一下你需要哪些软件包.因为你正试图GET
从URL
你需要的函数中读取一个函数net/http
.特别是:FormValue(key string)
退货GET
和POST
参数.
第二:现在打开appengine/datastore
文档并查找执行以下操作的函数:
string
将a 解码为a *datastore.Key
(DecodeKey(编码字符串))现在这很简单:
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
}
}
Run Code Online (Sandbox Code Playgroud)