gohttp/server/result.go

37 lines
662 B
Go
Raw Permalink Normal View History

2023-12-13 17:59:14 +08:00
package server
import "encoding/json"
type RestResult struct {
Code int `json:"code"`
Message string `json:"message"`
Error string `json:"error"`
Data interface{} `json:"data"`
}
var ResultSuccess = RestResult{
Code: 200,
Message: "success",
Data: nil,
}
var ResultError = RestResult{
Code: 500,
Message: "error",
Error: "error",
}
func NewErrorResult(err error) []byte {
result := ResultError
result.Error = err.Error()
ret, _ := json.Marshal(result)
return ret
}
func NewSuccessResult(data interface{}) []byte {
result := ResultSuccess
result.Data = data
ret, _ := json.Marshal(result)
return ret
}