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