使用结构体嵌套后如何写被嵌套的结构体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:"-"`
...
}
以上写法是无效的,望指点。
推荐文章: