Closed
Description
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
go1.7.5
While unmarshaling json into a struct, no error is thrown if the struct doesn't contain a particular field. For example if three fields are needed in the struct and the json input provides only two, no error is thrown and the third field is just empty.
this is my example:
type Example struct {
Name string `json:"name"`
Place string `json:"place"`
Date string `json:"date"`
}
func main() {
jsonStr := []byte(`{"name":"someName","place":"somePlace"}`)
var ex *Example
err := json.Unmarshal(jsonStr, &ex)
if err != nil {
fmt.Printf("ERROR : %v\n", err)
}
fmt.Printf("%#v",ex)
}
Output:
&main.Example{Name:"someName", Place:"somePlace", Date:""}
Expected:
Some error indicating required field not provided
I know I can check if the field is empty after unmarshaling, but is there a way to throw error during it?