當前位置: 首頁>>代碼示例>>Golang>>正文


Golang datastore.Client類代碼示例

本文整理匯總了Golang中google/golang.org/cloud/datastore.Client的典型用法代碼示例。如果您正苦於以下問題:Golang Client類的具體用法?Golang Client怎麽用?Golang Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Client類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: AddTask

// AddTask adds a task with the given description to the datastore,
// returning the key of the newly created entity.
func AddTask(ctx context.Context, client *datastore.Client, desc string) (*datastore.Key, error) {
    task := &Task{
        Desc:    desc,
        Created: time.Now(),
    }
    key := datastore.NewIncompleteKey(ctx, "Task", nil)
    return client.Put(ctx, key, task)
}
開發者ID:wuman,項目名稱:golang-samples,代碼行數:10,代碼來源:tasks.go

示例2: newDatastoreDB

// newDatastoreDB creates a new BookDatabase backed by Cloud Datastore.
// See the cloud and google packages for details on creating a suitable context:
// https://godoc.org/google.golang.org/cloud
// https://godoc.org/golang.org/x/oauth2/google
func newDatastoreDB(client *datastore.Client) (BookDatabase, error) {
    ctx := context.Background()
    // Verify that we can communicate and authenticate with the datastore service.
    t, err := client.NewTransaction(ctx)
    if err != nil {
        return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
    }
    if err := t.Rollback(); err != nil {
        return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
    }
    return &datastoreDB{
        client: client,
    }, nil
}
開發者ID:wuman,項目名稱:golang-samples,代碼行數:18,代碼來源:db_datastore.go

示例3: MarkDone

// [START update_entity]
// MarkDone marks the task done with the given ID.
func MarkDone(ctx context.Context, client *datastore.Client, taskID int64) error {
    // Create a key using the given integer ID.
    key := datastore.NewKey(ctx, "Task", "", taskID, nil)

    // In a transaction load each task, set done to true and store.
    _, err := client.RunInTransaction(ctx, func(tx *datastore.Transaction) error {
        var task Task
        if err := tx.Get(key, &task); err != nil {
            return err
        }
        task.Done = true
        _, err := tx.Put(key, &task)
        return err
    })
    return err
}
開發者ID:wuman,項目名稱:golang-samples,代碼行數:18,代碼來源:tasks.go

示例4: ListTasks

// [START retrieve_entities]
// ListTasks returns all the tasks in ascending order of creation time.
func ListTasks(ctx context.Context, client *datastore.Client) ([]*Task, error) {
    var tasks []*Task

    // Create a query to fetch all queries, ordered by "created".
    query := datastore.NewQuery("Task").Order("created")
    keys, err := client.GetAll(ctx, query, &tasks)
    if err != nil {
        return nil, err
    }

    // Set the id field on each Task from the corresponding key.
    for i, key := range keys {
        tasks[i].id = key.ID()
    }

    return tasks, nil
}
開發者ID:wuman,項目名稱:golang-samples,代碼行數:19,代碼來源:tasks.go

示例5: isValidNewConfig

func (fe *FeServiceImpl) isValidNewConfig(ctx context.Context, dsClient *datastore.Client, config *api.BiosphereCreationConfig) (bool, error) {
    if config == nil {
        return false, nil
    }
    if config.Name == "" || config.Nx <= 0 || config.Ny <= 0 {
        return false, nil
    }
    // Name must be unique.
    qSameName := datastore.NewQuery("BiosphereMeta").Filter("Name =", config.Name)
    numSameName, err := dsClient.Count(ctx, qSameName)
    if err != nil {
        return false, err
    }
    if numSameName > 0 {
        return false, nil
    }
    return true, nil
}
開發者ID:xanxys,項目名稱:bonsai,代碼行數:18,代碼來源:handle_add_biosphere.go

示例6: DeleteTask

// [START delete_entity]
// DeleteTask deletes the task with the given ID.
func DeleteTask(ctx context.Context, client *datastore.Client, taskID int64) error {
    return client.Delete(ctx, datastore.NewKey(ctx, "Task", "", taskID, nil))
}
開發者ID:wuman,項目名稱:golang-samples,代碼行數:5,代碼來源:tasks.go


注:本文中的google/golang.org/cloud/datastore.Client類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。