如何在Go Lang中将实体追加到数据存储

Need info on how to append an entity in Google cloud data store.

Put function is always overwritten with new values.

Any suggestions would be helpful.

09-OCT-2017:

I Used below code, but still updating entity instead of appending.(its erasing old & updated with new value, but i want to retain both values)

client, err := datastore.NewClient(ctx, projectID)
tx, err := client.NewTransaction(ctx)
if err != nil {
    log.Fatalf("Failed to create client: %v", err)
}

fmt.Fprint(w, input)
taskKey := datastore.NameKey("Entity", "stringID", nil)
var task Entity
if err := tx.Get(taskKey, &task); err != nil {
    log.Fatalf("tx.Get: %v", err)
}
task.Value = input
if _, err := tx.Put(taskKey, &task); err != nil {
    log.Fatalf("tx.Put: %v", err)
}
if _, err := tx.Commit(); err != nil {
    log.Fatalf("tx.Commit: %v", err)
}

There can be only one instance, only one entity bound / denoted by the same datastore key.

And entities (bound to a key) can only be overwritten, not updated / extended / appended gradually.

So if you already have an entity saved, to update/ modify it, you have to first load it, then modify the entity in memory, and write out (save) the modified entity. This save will overwrite the existing entity in the datastore.

If for a property you want to store multiple values, the type of that property must support storing multiple values. Slices in Go are such types.

So in your example your entity should look like this:

type Entity struct {
    Values []string 
}

When you load an existing Entity, you have to append the new value to its Values field, something like this (in pseudo code):

e := ... // load existing entity
e.Values = append(e.Values, input) // Append new data to Values
// And now save Entity (e) with the same key

In code:

client, err := datastore.NewClient(ctx, projectID)
tx, err := client.NewTransaction(ctx)
if err != nil {
    log.Fatalf("Failed to create client: %v", err)
}

fmt.Fprint(w, input)
taskKey := datastore.NameKey("Entity", "stringID", nil)
var task Entity
if err := tx.Get(taskKey, &task); err != nil {
    log.Fatalf("tx.Get: %v", err)
}
task.Values = append(task.Values, input)
if _, err := tx.Put(taskKey, &task); err != nil {
    log.Fatalf("tx.Put: %v", err)
}
if _, err := tx.Commit(); err != nil {
    log.Fatalf("tx.Commit: %v", err)
}

If you need to index by this Values property, you might run into troubles if it contains many values. See this possible duplicate for more details: App Engine Datastore: How to set multiple values on a property using golang?

If you run into this problem, you should consider modeling and storing your data in a different format, e.g. saving in multiple entities, where one entity would only store a single input, connected with the key it belongs to.

You should:

  • get an entity first,
  • then change a value
  • and after that update the entity in the datastore.

Please consult with docs: https://cloud.google.com/datastore/docs/concepts/entities#updating_an_entity:

To update an existing entity, modify the properties of the entity previously retrieved and store it using the key:

tx, err := client.NewTransaction(ctx)
if err != nil {
        log.Fatalf("client.NewTransaction: %v", err)
}
var task Task
if err := tx.Get(taskKey, &task); err != nil {
        log.Fatalf("tx.Get: %v", err)
}
task.Priority = 5
if _, err := tx.Put(taskKey, task); err != nil {
        log.Fatalf("tx.Put: %v", err)
}
if _, err := tx.Commit(); err != nil {
        log.Fatalf("tx.Commit: %v", err)
}