Firebase, Firego and orderBy

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

Firebase, Firego and orderBy

问题

我正在尝试使用Golang和Firego从Firebase获取一个按时间戳排序的元素列表。

文档建议使用以下代码:

var v map[string]interface{}
if err := f.StartAt("a").EndAt("c").LimitToFirst(8).OrderBy("field").Value(&v); err != nil {
    log.Fatal(err)
}
fmt.Printf("%s\n", v)

我可能漏掉了一些非常明显的东西,但是 v 不会是无序的吗?当我遍历这个映射时(for key, val := range v),值的顺序将不会与调用Firebase的响应中发送的顺序相同,因为访问的顺序是未定义的

我漏掉了什么?谢谢。

英文:

I'm trying to get a timestamp ordered list of elements out of Firebase, using Golang and Firego.

The documentation suggests:

var v map[string]interface{}
if err := f.StartAt("a").EndAt("c").LimitToFirst(8).OrderBy("field").Value(&v); err != nil {
    log.Fatal(err)
}
fmt.Printf("%s\n", v)

I must be missing something completely obvious, but isn't v going to be unordered? When I loop through the map (for key, val := range v) the values won't be in the same order as they have been sent in the response of the call to Firebase, since the order of access is undefined.

What am I missing? Thanks

答案1

得分: 1

结果的映射将是无序的,因为它是一个映射,但原始结果(限制为前8个)在限制之前将被排序,所以顺序可能非常重要。

我同意,对于这种结果来说,映射是一个不好的类型,他们可能使用映射是因为结果以json格式返回(尽管json有顺序,不像Go的映射)。他们应该返回一个结果数组以保持顺序。

英文:

The map of results will be unordered since it is a map, but the original results (limited to top 8) will be ordered before the limit, so the order could be very important.

I agree a map is a bad type for results of this kind, they're probably using that because results come back as json (though that does have an order, unlike go's map). They should be returning an array of results to preserve order.

huangapple
  • 本文由 发表于 2017年3月17日 04:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/42844648.html
匿名

发表评论

匿名网友

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

确定