Go API 的 KEY_RESERVED_PROPERTY 等价物是什么?数据存储

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

我需要检查密钥(即用户名)是否存在。 It seems KEY_RESERVED_PROPERTY 是可用于 java api 的特殊密钥,您可以使用它来实现最佳性能和强一致性,所以我想知道是否有任何等效项在去。

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

如果你look at the docsKEY_RESERVED_PROPERTY只不过是一个属性来引用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.

所以这没什么神奇的,您可以使用 __key__ 属性 在 Go 中做同样的事情,如 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)

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

您也可以尝试使用 datastore.Get() 函数按键加载实体。 return 值为 ErrNoSuchEntity 表示不存在具有指定键的实体:

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