Adding some additional json notes
continuous-integration/drone/push Build is passing Details

main
Drew Bednar 4 weeks ago
parent a79876bdda
commit 2a3e657a82

@ -56,6 +56,20 @@ Uses [CleanURLs](https://en.wikipedia.org/wiki/Clean_URL)
The pattern `err := json.NewEncoder(w).Encode(data)` looks elegant but creates issues when needing to coditionally set headers in response to an error. The performance difference between `json.Marshal` and `json.Encoder` are so small(tiny more mem and 1 extra alloc) that it's just cleaner to use `json.Marshal`.
### How Go Marshals JSON
When Go is encoding a particular type to JSON, it looks to see if the type has a MarshalJSON() method implemented on it. If it has, then Go will call this method to determine how to encode it.
Here is the interface
```
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
```
If the type doesnt have a MarshalJSON() method, then Go will fall back to trying to encode it to JSON based on its own internal set of rules.
### Enveloping Respsonses

Loading…
Cancel
Save