Go API中的KEY_RESERVED_PROPERTY等效于什么?datastore

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

What's the KEY_RESERVED_PROPERTY equivalent for the Go API ? datastore

问题

我需要检查一个键(即用户名)是否存在。似乎 KEY_RESERVED_PROPERTY 是一个特殊的键,可以在 Java API 中使用,以实现最佳性能和强一致性,所以我想知道在 Go 中是否有相应的等效方法。

目前,我正在考虑使用以用户名为祖先的查询 + KeysOnly()。

英文:

I need to check the existence of a key (i.e. an username). It seems that KEY_RESERVED_PROPERTY is a special key available for the java api that you can use to achieve the best performance and strong consistency so I'm wondering if there is any equivalent in Go.

Currently I'm considering using a query with the username as ancestor + KeysOnly().

答案1

得分: 0

如果你查看文档,KEY_RESERVED_PROPERTY只是一个用于引用实体键的属性:

一个保留的属性名称,用于引用实体的键。可以使用该字符串来对实体键进行过滤和排序。

所以这并没有什么神奇的,你可以在Go中使用__key__属性做同样的事情,正如文档中所述:

键过滤器

要根据实体键的值进行过滤,请使用特殊属性__key__

q := datastore.NewQuery("Person").Filter("__key__ >", lastSeenKey)

英文:

If you look at the docs, KEY_RESERVED_PROPERTY is nothing but a property to refer to the key:

> A reserved property name used to refer to the key of the entity. This string can be used for filtering and sorting by the entity key itself.

So this is nothing magical, you could do the same thing in Go with the __key__ property, as stated in the docs:

> ### Key filters
>
> To filter on the value of an entity's key, use the special property __key__:
>
> q := datastore.NewQuery("Person").Filter("__key__ >", lastSeenKey)

答案2

得分: 0

我需要检查一个键(即用户名)是否存在。

你可以通过使用datastore.Get()函数尝试加载键来实现。如果返回值是ErrNoSuchEntity,则表示没有与指定键对应的实体存在:

if err := datastore.Get(c, key, dst); err == datastore.ErrNoSuchEntity {
    // 键不存在
}
英文:

> I need to check the existence of a key (i.e. an username).

You can also do that by attempting to load the entity by key using the datastore.Get() function. A return value of ErrNoSuchEntity means no entity exists with the specified key:

if err := datastore.Get(c, key, dst); err == datastore.ErrNoSuchEntity {
    // Key doesn't exist
}

huangapple
  • 本文由 发表于 2015年4月10日 09:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/29551986.html
匿名

发表评论

匿名网友

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

确定