I believe with generics now in place a new more convenient Decode function could be added to the encoding/json package, amongst other encoding/* packages.
Proposed:
func handler(w http.ResponseWriter, r *http.Request) {
request, err := json.NewDecoder(r.Body).DecodeInto[MyStruct]()
if err != nil {
...
}
...
}
Existing:
func handler(w http.ResponseWriter, r *http.Request) {
var request MyStruct
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
...
}
...
}
The benefit of this API is not only is it more concise, but it removes the possibility of a json: Unmarshal(nil) error.
I believe with generics now in place a new more convenient
Decodefunction could be added to theencoding/jsonpackage, amongst otherencoding/*packages.Proposed:
Existing:
The benefit of this API is not only is it more concise, but it removes the possibility of a
json: Unmarshal(nil)error.