英文:
How to pull the auto generated unique ID after push in Firebase with Golang?
问题
我正在使用firego对Firebase进行操作。在成功推送后,我无法检索到自动生成的唯一ID。我使用了以下代码:
adminData, err := dB.Child("Admins").Push(m)
if err != nil {
log.Println(err)
}
log.Println("唯一ID: ", adminData)
它打印出:
https://myApp.firebaseio.com/Admins/-KddtEfpE3ys4cj0mYE-/.json
变量adminData的类型是*Firebase
但是我只需要-KddtEfpE3ys4cj0mYE-
,类型为String
。我该如何在Golang中使用firego实现这个目标?
英文:
I am using firego to do operations on the firebase. I am unable to retrieve the auto generated unique ID after a successful push. I used the fllowing code:
adminData, err := dB.Child("Admins").Push(m)
if err != nil {
log.Println(err)
}
log.Println("Unique ID: ", adminData)
and it print:
https://myApp.firebaseio.com/Admins/-KddtEfpE3ys4cj0mYE-/.json
the variable adminData is of type *Firebase
but I need only -KddtEfpE3ys4cj0mYE-
as type String
How I can do this in Golang using firego?
答案1
得分: 1
firego不提供这些信息,你可以在GitHub上创建一个问题,或者自己修改库。
另外,还有一种简单粗暴的方法,通过"/"字符进行分割,然后获取倒数第二个元素。
package main
import (
"fmt"
"strings"
)
func main() {
parts := strings.Split("https://myApp.firebaseio.com/Admins/-KddtEfpE3ys4cj0mYE-/.json", "/")
fmt.Println(parts[len(parts)-2]) // -KddtEfpE3ys4cj0mYE-
}
不过我建议你还是向firego提出一个问题
英文:
firego does not expose this information, you can either create an issue to firego repository on github or modify the library yourself.
also there is a dirty way to split by /
character and get last but one element.
package main
import (
"fmt"
"strings"
)
func main() {
parts := strings.Split("https://myApp.firebaseio.com/Admins/-KddtEfpE3ys4cj0mYE-/.json", "/")
fmt.Println(parts[len(parts)-2]) // -KddtEfpE3ys4cj0mYE-
}
Though I would recommend you to open an issue to firego
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论