LoginSignup
0
0

More than 3 years have passed since last update.

Go: Cloud Firestore のデータを削除 (Delete)

Last updated at Posted at 2020-12-17
firestore_delete.go
// ---------------------------------------------------------------
//
//  firestore_delete.go
//
//                  Dec/16/2020
//
// ---------------------------------------------------------------
package main

import (
    "context"
    "fmt"
    "log"
    "os"

    firebase "firebase.google.com/go"
    "google.golang.org/api/option"
)

// ---------------------------------------------------------------
func main() {
    fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
    key_in := os.Args[1]
    fmt.Printf ("key_in = %s\n" , key_in)
    // 初期化
    ctx := context.Background()
    sa := option.WithCredentialsFile("./application_default_credentials.json")
    app, err := firebase.NewApp(ctx, nil, sa)
    if err != nil {
        log.Fatalln(err)
    }

    client, err := app.Firestore(ctx)
    if err != nil {
        log.Fatalln(err)
    }

_, err = client.Collection("cities").Doc(key_in).Delete(ctx)
if err != nil {
        // Handle any errors in an appropriate way, such as returning them.
        log.Printf("An error has occurred: %s", err)
}

    // 切断
    defer client.Close()

    fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------

実行コマンド

project_id="project-dec16-2020"
echo $project_id
export GOOGLE_CLOUD_PROJECT=$project_id
#
go run firestore_delete.go t0923
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0