February 2019
Intermediate to advanced
450 pages
11h 59m
English
As the convention in JSON is for map keys to be in lowercase, we add hints to our struct of the `json:"subject"` form that tell the json package how to handle the field names within the struct. The updated definition should look like the following code:
type EmailMessage struct { Subject string `json:"subject"` Content string `json:"content"` To Email `json:"to"` From Email `json:"from"` Date time.Time `json:"sent"`}
To aid in testing, let's also add a String() function to the definition for easier debugging later:
func (e *EmailMessage) String() string { format := "EmailMessage{\n To:%s\n From:%s\n Subject:%s\n Date:%s\n}" return fmt.Sprintf(format, e.To, e.From, e.Subject, e.Date.String())}
Once this is in place, we can add some code ...
Read now
Unlock full access