英文:
Convert struct to map annotated with `firestore:"field_name"`
问题
我有一个结构体 -
type User struct {
Uid string `firestore:"uid"`
FcmToken string `firestore:"fcmtoken"`
}
如何使用 json.Marshal(user)
将其转换为映射(map)?
我知道当结构体字段用 json:"字段名"
注释时可以实现,但我不知道当它用 firestore 注释时该如何实现,或者是否可能实现?
我使用了术语“注释”,这可能不是它的正确称呼,请纠正我!
英文:
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!
答案1
得分: 1
一个字段标签可以包含多个键值对。请参阅struct标签文档了解更多细节。
编辑字段标签以包含您想要的任何JSON配置:
type User struct {
Uid string `firestore:"uid" json:"uid"`
FcmToken string `firestore:"fcmtoken" json:"tid"`
}
无法使JSON包使用firestore标签,反之亦然。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论