1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package config
import ( "encoding/json" "net/http" )
var ( OK = response(http.StatusOK, "ok") )
type Response struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` }
func (res *Response) WithMsg(message string) Response { return Response{ Code: res.Code, Msg: message, Data: res.Data, } }
func (res *Response) WithData(data interface{}) Response { return Response{ Code: res.Code, Msg: res.Msg, Data: data, } }
func (res *Response) ToString() string { err := &struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` }{ Code: res.Code, Msg: res.Msg, Data: res.Data, } raw, _ := json.Marshal(err) return string(raw) }
func response(code int, msg string) *Response { return &Response{ Code: code, Msg: msg, Data: nil, } }
|