![](/img/trans.png)
[英]google.golang.org/appengine/datastore vs cloud.google.com/go/datastore
[英]How to use cloud.google.com/go/datastore with AppEngine?
我正在將我的 Golang AppEngine 應用程序遷移到 1.12+,我需要切換到cloud.google.com/go/datastore
。 我不清楚如何將它與 AppEngine 一起使用,有人可以驗證我的假設嗎?
我的假設是我可以在main()
內部的某個地方運行(注意context.Background()
):
db, err := datastore.NewClient(context.Background(), datastore.DetectProjectID)
if err != nil {
panic(err)
}
defer db.Close()
然后我可以從我的處理程序中使用該數據庫:
func blah(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
key := datastore.NameKey("blah", blah, nil)
db.Get(ctx, key, blah2)
}
我對么? 或者我是否需要與每個 web 處理程序分開運行datastore.NewClient()
?
我為每個 web 處理程序運行datastore.NewClient()
。 我只為每個 web 處理程序執行一次,並且僅當該代碼需要使用數據存儲時。 另外,請確保在處理程序的末尾對它進行Close()
處理,否則會泄漏 memory。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.