我有这两个代表相同实体的结构(一个来自 Json 文件,另一个来自数据库)
type DriverJson struct {
ID int `json:"id"`
Name string `json:"name"`
}
type DriverOrm struct {
ID int `orm:"column(id);auto"`
Name string `orm:"column(name);size(255);null"`
}
我想将它们合并为一个驱动结构,如何合并映射符号(orm:, json:)?
谢谢
最佳答案
如 reflect.StructTag
的文档中所述,按照惯例,标记字符串的值是空格分隔的 key:"value"
对,因此很简单:
type DriverJson struct {
ID int `json:"id" orm:"column(id);auto"`
Name string `json:"name" orm:"column(name);size(255);null`
}
有关详细信息,请参阅 What are the use(s) for tags in Go?
关于go - 具有多个映射符号的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590571/