I'm seeking a clean approach to embed the return value of a method into the JSON marshalling of a type/value. It would be great if I don't need to write custom JSON marshaller.
For example, if the User type has FirstName
and LastName
fields and a FullName()
method, how can I easily embed a full_name
field into JSON output?
type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
func (u User) FullName() string {
return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}
Expected JSON:
{
"first_name": "John",
"last_name": "Smith",
"full_name": "John Smith"
}