使用结构体嵌套后如何写被嵌套的结构体tags
在学习的过程中突然有一个需求
type A struct{
UserID uint
Name string
}
type B struct{
A
...
}
此时我希望有个 tag 在 B 结构体中将 A 结构的 UserID成员在输出json时忽略
当然我有其他办法,比如获取数据后删除
但这个问题我是希望能在结构体中解决
我尝试覆盖,代码如下
type A struct{
UserID uint
Name string
}
type B struct{
A
UserID uint `json:"-"`
...
}
以上写法是无效的,望指点。
关于 LearnKu
@嘉宝君 B加个UserID不赋值,json使用omitempty忽略0值