英文:
How to create a push id for firebase using golang?
问题
我想将一个带有ID的地图推送到Firebase(例如:-KfKoScgRhylaLjQlK-y)。
fitToWorkMap := make(map[string]models.TaskFitToWork)
fitToWorkForTask := models.TaskFitToWork{}
for i := 0; i < len(FitToWorkSlice); i++ {
fitToWorkForTask.Info = FitToWorkSlice[i]
fitToWorkForTask.DateOfCreation = time.Now().Unix()
fitToWorkForTask.Status = helpers.StatusPending
fitToWorkMap["fgsgdsfn+'i'"] = fitToWorkForTask
}
task.FitToWork = fitToWorkMap
这里的fitToWorkMap
是一个地图。我想为这个地图生成一个键。
英文:
i want to push a map to firebase with an id (eg:-KfKoScgRhylaLjQlK-y)
fitToWorkMap := make(map[string]models.TaskFitToWork)
fitToWorkForTask :=models.TaskFitToWork{}
for i := 0; i < len(FitToWorkSlice); i++ {
fitToWorkForTask.Info =FitToWorkSlice[i]
fitToWorkForTask.DateOfCreation =time.Now().Unix()
fitToWorkForTask.Status = helpers.StatusPending
fitToWorkMap["fgsgdsfn+'i'"] = fitToWorkForTask
}
task.FitToWork = fitToWorkMap
here fitToWorkMap is a map .i want to generate a key for this map
答案1
得分: 1
以-K
开头的那些键被称为推送ID,它们是由Firebase自动生成的:
- 当你在支持的SDK中调用
push()
或childByAutoId
时 - 当你通过REST API执行
POST
请求时
Go没有Firebase SDK,它使用REST API与Firebase数据库进行交互。这意味着它只在向数据库POST
新节点时生成推送ID。我不知道有任何用于在Go中客户端生成Firebase推送ID的库。
但幸运的是,这些键在一篇博文中有很好的文档记录。还有一个JavaScript实现generatePushID()
的代码可用,所以你可以将其移植到Go中。
英文:
Those keys starting with -K
are called push IDs and they are automatically generated by Firebase:
- When you call
push()
orchildByAutoId
in one of the supported SDKs - When you perform a
POST
request to the REST API
There is no Firebase SDK for Go and uses the REST API to interact with the Firebase Database. That means that it only generates a push ID when it POST
s a new node to the database. I don't know of any library for Go to generate Firebase push IDs client-side.
But luckily the keys are quite well documented in a blog post. The code for a JavaScript implementation of generatePushID()
is also available, so you could port that over to Go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论