目錄
將 firestore'integer_value”轉換為整數
問題內容
使用 golang firestore 1.8 函式庫,我嘗試使用 google 去年秋天推出的 firestore 的新 count() 函式。這些文件似乎還沒有範例,不是我發現的,但我拼湊了一些有點可行的程式碼,這些程式碼幾乎讓我完成了所有工作,只是沒有實際產生一個整數。該片段底部的“result[usercountalias]”值是我感興趣的轉換為整數的值,但我不太確定如何轉換。當然,我可以將它作為一個字串,在冒號上拆分,然後解析它,但這看起來很難看。
任何提示將不勝感激!
非常感謝。
func (s UserService) Count(labID string) (int64, error) { if s.DB == nil { return -1, customerrors.ErrDatabaseMissing } query := s.DB. Collection(CollectionUsers). Where("lab_id", "==", labID) userCountAlias := "userCount" ag := query.NewAggregationQuery() //result is a firestore.AggregationResult, which seems to consist of just a //map[string]interface{} result, err := ag.WithCount(userCountAlias).Get(s.Ctx) if err != nil { return -1, err } v := result[userCountAlias]//How do I cast this to an integer? fmt.Printf("Type = %v", v) //Prints "Type = integer_value:379" return -1, nil }
登入後複製
正確答案
嘗試fmt.printf("type = %t", v)
找出v
#的類型。
v
最有可能是 firestorepb.value。請注意,這在 1.8 中尚不可用。試著將 cloud.google.com/go/firestore
升級到最新版本(目前為 1.9)。
package main import ( "fmt" "cloud.google.com/go/firestore/apiv1/firestorepb" ) func main() { var v interface{} = &firestorepb.Value{ ValueType: &firestorepb.Value_IntegerValue{ IntegerValue: 379, }, } fmt.Printf("%T\n", v) // *firestorepb.Value fmt.Printf("%v\n", v) // integer_value:379 if v, ok := v.(*firestorepb.Value); ok { fmt.Printf("%v\n", v.GetIntegerValue()) // 379 } }
登入後複製
官方儲存庫中的測試以相同的方式檢索值。請參閱 testintegration_countaggregationquery。
以上是將 firestore'integer_value”轉換為整數的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)