Here has an interface SpecificTemplate
nested inside struct Template
:
package main
import (
"encoding/json"
"fmt"
)
type SpecificTemplate interface {
GetLabel() string
}
type TemplateA struct {
Label string `json:"label"`
}
func (t TemplateA) GetLabel() string {
return t.Label
}
type Template struct {
Id int64 `json:"id"`
SpecificTemplate
}
func main() {
ta := TemplateA{
Label: `label1`,
}
t := Template{
Id: 1,
SpecificTemplate: ta,
}
data, _ := json.Marshal(t)
fmt.Println(string(data))
}
It would be
{
"id":1,
"SpecificTemplate":{
"label":"label1"
}
}
I wanna kown how to show json at same level, just like:
{
"id":1,
"label":"label1"
}