Below is my take on reducing error handling on for Go, sample is for when getting HTTP URL parameters :
(Design pattern derived from https://blog.golang.org/errors-are-values)
type HTTPAdapter struct {
Error *common.AppError
}
func (adapter *HTTPAdapter) ReadUUID(r *http.Request, param string, possibleError int) uuid.UUID {
requestUUID := uuid.Parse(mux.Vars(r)[param])
if requestUUID == nil {
adapter.Error = common.NewAppError(fmt.Errorf("parameter %v is not valid", param),
possibleError, http.StatusBadRequest)
}
return requestUUID
}
calling it for multiple possible parameters would be as below:
adapter := &httphelper.HTTPAdapter{}
viewingID := adapter.ReadUUID(r, "viewingID", common.ErrorWhenReadingViewingID)
messageID := adapter.ReadUUID(r, "messageID", common.ErrorWhenReadingMessadeID)
if adapter.Error != nil {
return nil, adapter.Error
}
This is not a silver bullet, the downside is if you had multiple errors you are only able to get the last error.
But in this instance it is relatively repetitive and low risk, hence I can just get the last possible error.