beegoinit/response/xml.go
liangzy eba7a63f4b 两项优化
1. 优化json类
2. 优化数据库默认连接配置
2020-08-11 14:31:27 +08:00

49 lines
902 B
Go

package response
import (
"bytes"
"encoding/xml"
"log"
)
type Xml struct {
Code int `xml:"code"`
Data interface{} `xml:"data"`
Msg string `xml:"msg"`
Status bool `xml:"status"`
}
func (x *Xml) String() string {
b, err := xml.MarshalIndent(x, "", " ")
if err != nil {
log.Println("xml encode", err)
}
return xml.Header + bytes.NewBuffer(b).String()
}
func NewXmlByDefaultSuccess(data ...interface{}) Xml {
return NewXml(true, 0, JsonMsgDefaultOk, data...)
}
func NewXmlByDefaultFailed(data ...interface{}) Xml {
return NewXml(false, 1, JsonMsgDefaultFailed, data...)
}
func NewXml(status bool, code int, msg string, data ...interface{}) Xml {
if len(data) > 0 {
return Xml{
Code: code,
Data: data[0],
Msg: msg,
Status: status,
}
} else {
return Xml{
Code: code,
Data: nil,
Msg: msg,
Status: status,
}
}
}