You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool-biz/core/nstatus/result.go

46 lines
1.0 KiB
Go

1 year ago
package nstatus
import (
"encoding/json"
"fmt"
)
// Result 统一返回结果 结构体
// {"code":200,"msg":"OK","data":{}}
// API RPC 共用
type Result struct {
Code int `json:"code"` // 返回编码(业务+错误)
Msg string `json:"msg"` // 消息
Data any `json:"data,omitempty,optional"` // 数据
}
func NewResult(code int, message string) *Result {
return NewResultWithData(code, message, nil)
}
func NewResultWithData(code int, message string, data any) *Result {
return &Result{
Code: code,
Msg: message,
Data: data,
}
}
// Error 错误输出,同时可作为错误
func (r *Result) Error() string {
return r.String()
}
func (r *Result) String() string {
return fmt.Sprintf("Code:%d Message:%s Data:%v", r.Code, r.Msg, r.Data)
}
// JsonString 返回JsonString格式
func (r *Result) JsonString() string {
bytes, err := json.Marshal(r)
if err != nil {
return ""
}
return string(bytes)
}