How to create a push id for firebase using golang?

huangapple go评论94阅读模式
英文:

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 &lt; len(FitToWorkSlice); i++ {
		fitToWorkForTask.Info =FitToWorkSlice[i]
		fitToWorkForTask.DateOfCreation =time.Now().Unix()
		fitToWorkForTask.Status = helpers.StatusPending

		fitToWorkMap[&quot;fgsgdsfn+&#39;i&#39;&quot;] = 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:

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 POSTs 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.

huangapple
  • 本文由 发表于 2017年3月20日 19:44:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/42902721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定