我有一个结构Base
:如何动态地在JSON中包含/排除结构体的字段?
type Base struct {
Name string `json:"name,omitempty"`
// ... other fields
}
,另外两个结构的,嵌入Base
:
type First struct {
Base
// ... other fields
}
type Second struct {
Base
// ... other fields
}
现在我要当元帅的结构First
和Second
但有一点差别。我想在First
中包含Name
字段,但我不想将其包含在Second
中。
或者为了简化问题,我想动态地在JSON中输入和输出struct的字段。
注意: Name
值总是有价值,我不想改变它。
不,它没有工作!它仍然存在! 即使它可以工作,它似乎有点令我困惑!它可能会引起一些问题! – mehdy