Skip to content

Instantly share code, notes, and snippets.

@ahhzaky
Created September 4, 2024 19:37
Show Gist options
  • Save ahhzaky/82ef4f341e306c0fe6be130d5fa50698 to your computer and use it in GitHub Desktop.
Save ahhzaky/82ef4f341e306c0fe6be130d5fa50698 to your computer and use it in GitHub Desktop.
helper/ golang template response api
package helper
func PanicIfError(err error) {
if err != nil {
panic(err)
}
}
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