Let's say we're doing a user registration. In most dynamic languages the JSON body will get parsed into a map. Excuse the fat controller and pseudo-language, but it'll end up looking something like:
func create(conn, params) do
if not Validator.is_email?(params["email"]) do
return error(conn, "email is not valid")
end
if not Validator.min_length?(params["password"], 10) do
return error(conn, "password must be 10 or more characters")
end
create_user(params)
true
end
In Go, if you want this nice and typed, you'd add a RegistrationInput struct with the field mapping tags: type RegistrationInput struct {
Email string `json:"email"`
Password string `json:"password"`
}
Map the user's input to the structure, handling the error: var input RegistrationInput
if err := json.Unmarshal(req.body, &input); err != nil {
...
}
And then write the exact same validation checks.What safety does Go's version buy you?