Custom JSON Marshaling in Go

Go’s standard library has a convenient way of handling JSON verification and default values through the Marshaler and Unmarshaler interfaces. This means we don’t necessarily need separate methods to handle this data verification/manipulation. Unmarshaler Let’s pretend our system can’t allow users that are under the age 13 and we need a default timezone. var ( ErrTooYoung = fmt.Errorf("too young") ErrTZNotFound = fmt.Errorf("invalid timezone, must be one of %v", timezones) ) type TimeZone string var timezones = [....

February 19, 2023 · 4 min · 717 words · John Hooks