1 Star 0 Fork 0

Sage / go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server_http_response.go 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
王少奇 提交于 2021-08-30 20:27 . 优化
package httpserver
import (
"encoding/json"
"net/http"
)
type IHttpResponse interface {
SetCode(code int64)
SetMsg(msg string)
SetData(data interface{})
SetRequestId(string)
GetCode() int64
GetMsg() string
GetData() interface{}
GetRequestId() string
Response(...interface{})
}
type HttpResponseData struct {
Code int64 `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"message"`
RequestId string `json:"requestId"`
}
func (d *HttpResponseData) Encode() []byte {
s, err := json.Marshal(d)
if err != nil {
return []byte("response marshal err " + err.Error())
}
return s
}
type HttpResponse struct {
writer http.ResponseWriter
respData *HttpResponseData
}
func NewHttpResponse(writer http.ResponseWriter) IHttpResponse {
return &HttpResponse{
writer: writer,
respData: &HttpResponseData{
RequestId: "",
},
}
}
func (resp *HttpResponse) GetCode() int64 {
return resp.respData.Code
}
func (resp *HttpResponse) GetMsg() string {
return resp.respData.Msg
}
func (resp *HttpResponse) GetData() interface{} {
return resp.respData.Data
}
func (resp *HttpResponse) GetRequestId() string {
return resp.respData.RequestId
}
func (resp *HttpResponse) SetCode(code int64) {
resp.respData.Code = code
}
func (resp *HttpResponse) SetMsg(msg string) {
resp.respData.Msg = msg
}
func (resp *HttpResponse) SetData(data interface{}) {
resp.respData.Data = data
}
func (resp *HttpResponse) SetRequestId(requestId string) {
resp.respData.RequestId = requestId
}
func (resp *HttpResponse) Response(params ...interface{}) {
resp.writer.Write(resp.respData.Encode())
}
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.45

搜索帮助