http://localhost:8080/?key=ahFkZXZ-ZGV2LWVkdW5hdGlvbnIOCxIIVXNlckluZm8YLAw
我想問一下如何:
- 解碼並轉換成「關鍵」的
*datastore.Key
- 並用它來獲得一個實體。
感謝您的幫助!
http://localhost:8080/?key=ahFkZXZ-ZGV2LWVkdW5hdGlvbnIOCxIIVXNlckluZm8YLAw
我想問一下如何:
*datastore.Key
感謝您的幫助!
第一:你應該考慮哪個包你需要這種情況。由於您正嘗試從URL
中讀取GET
的值,因此您可能需要使用net/http
中的函數。 特別是:FormValue(key string)
返回GET
和POST
參數。
二:現在打開appengine/datastore
文件並找到函數用於如下:
string
到*datastore.Key
(DecodeKey(encoded string))現在這是一件非常容易的事情:
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
}
}
您應該爲此問題添加更多標籤。 (Go and gae-datastore) – Testuser
如果你能向我們展示你之前試過的東西,這也很好:)(這是一個簡單的問題,很容易解決) – Testuser