func ExampleCommit_Key() { ctx := context.Background() client, err := datastore.NewClient(ctx, "") if err != nil { // TODO: Handle error. } var pk1, pk2 *datastore.PendingKey // Create two posts in a single transaction. commit, err := client.RunInTransaction(ctx, func(tx *datastore.Transaction) error { var err error pk1, err = tx.Put(datastore.NewIncompleteKey(ctx, "Post", nil), &Post{Title: "Post 1", PublishedAt: time.Now()}) if err != nil { return err } pk2, err = tx.Put(datastore.NewIncompleteKey(ctx, "Post", nil), &Post{Title: "Post 2", PublishedAt: time.Now()}) if err != nil { return err } return nil }) if err != nil { // TODO: Handle error. } // Now pk1, pk2 are valid PendingKeys. Let's convert them into real keys // using the Commit object. k1 := commit.Key(pk1) k2 := commit.Key(pk2) fmt.Println(k1, k2) }
func ExampleClient_PutMulti() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START batch_upsert] tasks := []*Task{ { Category: "Personal", Done: false, Priority: 4, Description: "Learn Cloud Datastore", }, { Category: "Personal", Done: false, Priority: 5, Description: "Integrate Cloud Datastore", }, } keys := []*datastore.Key{ datastore.NewIncompleteKey(ctx, "Task", nil), datastore.NewIncompleteKey(ctx, "Task", nil), } keys, err := client.PutMulti(ctx, keys, tasks) // [END batch_upsert] _ = err // Make sure you check err. _ = keys // keys now has the complete keys for the newly stored tasks. }
func ExampleNewIncompleteKey() { ctx := context.Background() // [START incomplete_key] taskKey := datastore.NewIncompleteKey(ctx, "Task", nil) // [END incomplete_key] _ = taskKey // Use the task key for datastore operations. }
func ExampleClient_Put() { ctx := context.Background() client, err := datastore.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } type Article struct { Title string Description string Body string `datastore:",noindex"` Author *datastore.Key PublishedAt time.Time } newKey := datastore.NewIncompleteKey(ctx, "Article", nil) _, err = client.Put(ctx, newKey, &Article{ Title: "The title of the article", Description: "The description of the article...", Body: "...", Author: datastore.NewKey(ctx, "Author", "jbd", 0, nil), PublishedAt: time.Now(), }) if err != nil { // TODO: Handle error. } }
func ExampleClient_Put_upsert() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") task := &Task{} // Populated with appropriate data. key := datastore.NewIncompleteKey(ctx, "Task", nil) // [START upsert] key, err := client.Put(ctx, key, task) // [END upsert] _ = err // Make sure you check err. _ = key // key is the complete key for the newly stored task }
func main() { ctx := context.Background() pubsubClient, err := pubsub.NewClient(ctx, "project") if err != nil { log.Fatalf("pubsub.NewClient: %v", err) } topic, err := pubsubClient.NewTopic(ctx, "topic") if err != nil { log.Fatalf("NewTopic: %v", err) } sub, err := pubsubClient.NewSubscription(ctx, "sub", topic, 0, nil) if err != nil { log.Fatalf("NewSubscription: %v", err) } if _, err := topic.Publish(ctx, &pubsub.Message{ Data: []byte("hello"), }); err != nil { log.Fatalf("Publish: %v", err) } it, err := sub.Pull(ctx) if err != nil { log.Fatalf("Pull: %v", err) } msg, err := it.Next() if err != nil { log.Fatalf("Next: %v", err) } log.Printf("pubsub message: %s", msg.Data) msg.Done(true) it.Stop() datastoreClient, err := datastore.NewClient(ctx, "project") if err != nil { log.Fatalf("datastore.NewClient: %v", err) } k := datastore.NewIncompleteKey(ctx, "Foo", nil) k, err = datastoreClient.Put(ctx, k, &Foo{F: "foo!"}) if err != nil { log.Fatalf("Put: %v", err) } var f Foo if err := datastoreClient.Get(ctx, k, &f); err != nil { log.Fatalf("Get: %v", err) } log.Printf("datastore got %v", f) }
func ExampleClient_AllocateIDs() { ctx := context.Background() client, err := datastore.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } var keys []*datastore.Key for i := 0; i < 10; i++ { keys = append(keys, datastore.NewIncompleteKey(ctx, "Article", nil)) } keys, err = client.AllocateIDs(ctx, keys) if err != nil { // TODO: Handle error. } _ = keys // TODO: Use keys. }
func ExampleClient_Put() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START entity_with_parent] parentKey := datastore.NewKey(ctx, "TaskList", "default", 0, nil) key := datastore.NewIncompleteKey(ctx, "Task", parentKey) task := Task{ Category: "Personal", Done: false, Priority: 4, Description: "Learn Cloud Datastore", } // A complete key is assigned to the entity when it is Put. var err error key, err = client.Put(ctx, key, &task) // [END entity_with_parent] _ = err // Make sure you check err. }
func ExampleNewIncompleteKey() { ctx := context.Background() k := datastore.NewIncompleteKey(ctx, "Article", nil) _ = k // TODO: Use incomplete key. }