Restful接口通用返回函数示例__Golang

const (
	MsgSuccess = "success"
	MsgFailed  = "failed"
	Null       = ""
)

type Response struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

// OK 返回成功信号
func OK() Response {
	return Response{
		Code: http.StatusOK,
		Msg:  MsgSuccess,
		Data: Null,
	}
}

// OKWithData 返回数据响应
func OKWithData(data any) Response {
	return Response{
		Code: http.StatusOK,
		Msg:  MsgSuccess,
		Data: data,
	}
}

// ErrWithMsg 返回错误响应
func ErrWithMsg(msg string) Response {
	return Response{
		Code: http.StatusInternalServerError,
		Msg:  msg,
		Data: Null,
	}
}

 

posted @ 2025-04-11 00:05  Ashe|||^_^  阅读(23)  评论(0)    收藏  举报