Skip to content

Instantly share code, notes, and snippets.

@pedrovasconcellos
Created April 28, 2023 20:19
Show Gist options
  • Save pedrovasconcellos/4de37edcf693e0a27b095535822a3796 to your computer and use it in GitHub Desktop.
Save pedrovasconcellos/4de37edcf693e0a27b095535822a3796 to your computer and use it in GitHub Desktop.
Result class in Go Language
type ResultT struct {
Response interface{}
Errors map[string]string
}
func NewResultT() ResultT {
return ResultT{
Response: nil,
Errors: make(map[string]string),
}
}
func WithResponse(response interface{}) ResultT {
return ResultT{
Response: response,
Errors: make(map[string]string),
}
}
func WithErrors(errors map[string]string) ResultT {
return ResultT{
Response: nil,
Errors: errors,
}
}
func WithResponseAndErrors(response interface{}, errors map[string]string) ResultT {
return ResultT{
Response: response,
Errors: errors,
}
}
func WithResponseAndError(response interface{}, parameterError string, error string) ResultT {
errors := make(map[string]string)
errors[parameterError] = error
return ResultT{
Response: response,
Errors: errors,
}
}
func (r *ResultT) AddError(parameter string, error string) {
r.Errors[parameter] = error
}
func (r *ResultT) ContainsErrorDescription(errorDescription string) bool {
for _, v := range r.Errors {
if v == errorDescription {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment