When decoding JSON I've always explicitly written a struct for each object so that I could implement the Stringer interface for individual objects in a parent struct like so:
type Data struct {
Records []Record
}
type Record struct {
ID int
Value string
}
func (r Record) String() string {
return fmt.Sprintf("{ID:%d Value:%s}", r.ID, r.Value)
}
I recently learned that it is possible to do nesting with anonymous structs. This method is much more concise for defining the structure of the data to be decoded:
type Data struct {
Records []struct {
ID int
Value string
}
}
But, is it possible to define a method on a member of a struct, particularly a member which is an anonymous struct? Like the Stringer interface implementation in the first code block.