您必须为此实现自定义json.Marshaler
。
type boo struct {
Boo1 string `json:"boo1"`
// "-" will tell encoding/json to ignore this field during (un)marshaling
Foo foo `json:"-"`
}
func (b boo) MarshalJSON() ([]byte, error) {
// Declare a new type using boo's definition, this
// "copies" boo's structure but not its behaviour,
// i.e. B has same fields as boo, but zero methods,
// not even MarshalJSON -- this is necessary to avoid
// infinite recursive calls to MarshalJSON.
type B boo
// Declare a new type that *embeds* those structs whose
// fields you want to be at the same level.
type T struct {
B
foo
}
// Create an instance of the new type with its fields
// set from the source boo instance and marshal it.
return json.Marshal(T{B: B(b), foo: b.Foo})
}
https://play.golang.org/p/Go1w9quPkMa
关于“匿名”的说明
您使用的匿名标签以及您引用的文档中使用的标签已过时且不精确。没有明确名称的字段的正确标签是嵌入的。
https://golang.org/ref/spec#Struct_types
使用类型声明但没有显式字段名称的字段称为嵌入字段。嵌入字段必须指定为类型名称 T 或指向非接口类型名称 *T 的指针,并且 T 本身可能不是指针类型。非限定类型名称充当字段名称。
区别很重要,因为 Go 中有“匿名结构字段”这样的东西,它经常使用,但它与嵌入式字段不同。例如:
type T struct {
// F is an anonymous struct field, or
// a field of an anonymous struct type.
// F is not embedded however.
F struct {
foo string
bar int
}
}