[Go] golang http下返回json数据
2021-02-21 01:17
标签:UNC form ons 定义 结构 request ring email rsh 需求返回json格式编码的结构体 , 需要返回content-type 返回不同的响应码 结构体的定义 ,因为可导出的结构体 ,必须大写,如果要小写 ,就得加这个别名 从post中获取到字段后 , 返回对应的结果 , 设置header必须在返回响应码之前调用 [Go] golang http下返回json数据 标签:UNC form ons 定义 结构 request ring email rsh 原文地址:https://www.cnblogs.com/taoshihan/p/12912599.htmltype JsonResult struct{
Code int `json:"code"`
Msg string `json:"msg"`
}
//验证接口
func check(w http.ResponseWriter, r *http.Request) {
email := r.PostFormValue("email")
server := r.PostFormValue("server")
password := r.PostFormValue("password")
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
w.Header().Set("content-type","text/json")
if email != "" && server != "" && password != "" {
res := tools.CheckEmailPassword(server, email, password)
if res {
msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功"})
w.Write(msg)
} else {
w.WriteHeader(400)
w.Write(msg)
}
} else {
w.WriteHeader(400)
w.Write(msg)
}
}
文章标题:[Go] golang http下返回json数据
文章链接:http://soscw.com/index.php/essay/58226.html