Home > Software engineering >  Convert struct to map annotated with `firestore:"field_name"`
Convert struct to map annotated with `firestore:"field_name"`

Time:11-09

I have a struct -

type User struct {
    Uid      string `firestore:"uid"`
    FcmToken string `firestore:"fcmtoken"
}

How do I convert It to map using json.Marshal(user) , I know it can be done when struct fields are annotated with json:"fieldname" but I don't know how to do the same when it is annotated with firestore, or is it even possible?

I have used the word annotation, which may not be what it is called, please correct me!

CodePudding user response:

A field tag can contain multiple key/value pairs. See the struct tag documentation for more details.

Edit the field tags to include whatever JSON configuration you want:

type User struct {
    Uid      string `firestore:"uid" json:"uid"`
    FcmToken string `firestore:"fcmtoken" json:"tid"`
}

It is not possible to make the JSON package use the firestore tags or vice versa.

  • Related