Created
September 4, 2024 19:37
-
-
Save ahhzaky/82ef4f341e306c0fe6be130d5fa50698 to your computer and use it in GitHub Desktop.
helper/ golang template response api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package helper | |
func PanicIfError(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package helper | |
import ( | |
"github.com/go-playground/validator/v10" | |
) | |
type Response struct { | |
Meta Meta `json:"meta"` | |
Data interface{} `json:"data"` | |
} | |
type Meta struct { | |
Message string `json:"message"` | |
Code int `json:"code"` | |
Status string `json:"status"` | |
} | |
func APIResponse(message string, code int, status string, data interface{}) Response { | |
meta := Meta{ | |
Message: message, | |
Code: code, | |
Status: status, | |
} | |
jsonResponse := Response{ | |
Meta: meta, | |
Data: data, | |
} | |
return jsonResponse | |
} | |
func FormatValidationError(err error) []string { | |
var errors []string | |
for _, e := range err.(validator.ValidationErrors) { | |
errors = append(errors, e.Error()) | |
} | |
return errors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment