1 Star 0 Fork 0

Souki/go-framework

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
server_http_response.go 1.61 KB
Copy Edit Raw Blame History
王少奇 authored 2021-08-30 20:27 +08:00 . 优化
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())
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.12

Search