1

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().

Community
  • 1
  • 1
The user with no hat
  • 10,166
  • 20
  • 57
  • 80

2 Answers2

0

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)

Community
  • 1
  • 1
Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41
  • Can this be used with KeysOnly() ? For some reasons I get no result / iteration or error which is quite strange – The user with no hat Apr 10 '15 at 02:50
  • If should work, maybe there's something off in your logic, you're stating in the question that you want to use the username as ancestor, if it's the root entity you should just query directly, if not then something else is the ancestor. – Jaime Gómez Apr 10 '15 at 03:06
0

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
}
icza
  • 389,944
  • 63
  • 907
  • 827