私はastructを持っていBase
ます:
type Base struct {
Name string `json:"name,omitempty"`
// ... other fields
}
そして、さらに2つの構造体が埋め込まれていBase
ます:
type First struct {
Base
// ... other fields
}
type Second struct {
Base
// ... other fields
}
今、私は元帥に構造体をしたいFirst
とSecond
少し違いが。Name
フィールドを含めたいのですが、フィールドにFirst
含めたくありませんSecond
。
または、質問を単純化するために、JSONの構造体フィールドを動的にオプトインおよびオプトアウトしたいと思います。
注:Name
値には常に値があり、変更したくありません。
Marshaler
タイプのインターフェイスを実装してSecond
、ダミータイプを作成できますSecondClone
。
type SecondClone Second
func (str Second) MarshalJSON() (byt []byte, err error) {
var temp SecondClone
temp = SecondClone(str)
temp.Base.Name = ""
return json.Marshal(temp)
}
これは、コードに他の変更を加えなくても機能します。
またName
、別のタイプ/コピーで機能するため、値は変更されません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加