# How to update account user metadata using Go?

**URL:** <https://forum.heroiclabs.com/t/how-to-update-account-user-metadata-using-go/3655>\
**Category:** Runtime Framework\
**Tags:** golang\
**Created:** [March 18, 2023, 6:40pm UTC](https://forum.heroiclabs.com/t/how-to-update-account-user-metadata-using-go/3655 "2023-03-18T18:40:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spuddy](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@Spuddy](https://forum.heroiclabs.com/u/Spuddy)\
**Post date:** [March 18, 2023, 6:40pm UTC](https://forum.heroiclabs.com/t/how-to-update-account-user-metadata-using-go/3655/1 "2023-03-18T18:40:49Z")

</div>

Does anybody have any Go server code they can share with me to show how to update the user’s metadata ? nk.AccountUpdateId seems to overwrite the metadata so I assume I have to read the existing metadata first and then add to it before writing it back with nk.AccountUpdateId ? I’m extremely new to GoLang so if someone’s already done this and could post their code then it would save me a lot of time. Thanks!

```auto
	userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
	if !ok {
		return "", runtime.NewError("could not get user ID from context", 13)
	}

	if err := nk.AccountUpdateId(ctx, userId, "", metadata, "", "", "", "", ""); err != nil {
		return "", runtime.NewError("could not update account", 13)
	}

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [March 20, 2023, 5:14pm UTC](https://forum.heroiclabs.com/t/how-to-update-account-user-metadata-using-go/3655/2 "2023-03-20T17:14:50Z")

</div>

Hello @Spuddy, you can modify the metadata as follows:

```auto
userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
    return "", runtime.NewError("could not get user ID from context", 13)
}
account, err := nk.AccountGetId(ctx, userId)
// err checking

var metadata map[string]any
err = json.Unmarshal([]byte(account.User.Metadata), metadata)
// err checking

// Apply modifications to metadata
metadata["foo"] = "bar"

if err := nk.AccountUpdateId(ctx, userId, "", metadata, "", "", "", "", ""); err != nil {
    return "", runtime.NewError("could not update account", 13)
}

```

While the above works, mind that if you expect concurrent updates, it does not guarantee the metadata will be consistent, in which case you should use a [storage object with conditional writes](https://heroiclabs.com/docs/nakama/concepts/storage/collections/#conditional-writes) instead.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Spuddy](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@Spuddy](https://forum.heroiclabs.com/u/Spuddy)\
**Post date:** [March 20, 2023, 10:03pm UTC](https://forum.heroiclabs.com/t/how-to-update-account-user-metadata-using-go/3655/3 "2023-03-20T22:03:56Z")

</div>

Thanks for the code. There shouldn’t be more than one device updating the same player’s metadata at the same time but do you think it’s okay to call the above code several times one after the other ? Do you know if the user’s metadata is updated on the server immediately before AccountUpdateId returns otherwise the second call to AccountGetId might return stale data ?

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [March 22, 2023, 4:37pm UTC](https://forum.heroiclabs.com/t/how-to-update-account-user-metadata-using-go/3655/4 "2023-03-22T16:37:41Z")

</div>

Sequential calls are fine, the issue may only occur if multiple calls are concurrently made within the same account. The server only updates the metadata when the respective runtime function is called.
