Slide 15
Slide 15 text
1-3. How to use Go SDK
15
● Transaction
ref := client.Collection("cities").Doc("SF")
err := client. RunTransaction (ctx, func(ctx context. Context, tx *firestore. Transaction) error
{
doc, err := tx. Get(ref) // tx.Get, NOT ref.Get!
if err != nil { return err }
pop, err := doc. DataAt("population")
if err != nil { return err }
// 👇更新処理中(tx.Set)に同時編集が影響する場合は、トランザクション関数が複数回実行される
return tx.Set(ref, map[string]interface{}{ "population": pop.(int64) + 1,},
firestore.MergeAll)
})
if err != nil { return err }
https://firebase.google.com/docs/firestore/manage-data/transactions#transaction