go的web服务器框架

net/http

golang内置的网络编程库功能已经很强大,但是没有参数解析和(json)类型封装等功能,不区分方法类型,如get。

go 复制代码
type Response struct {
  Code int    `json:"code"`
  Data any    `json:"data"`
  Msg  string `json:"msg"`
}

func get22(res http.ResponseWriter, req *http.Request) {
  // 获取参数
  fmt.Println(req.URL.String())
  byteData, _ := json.Marshal(Response{
    Code: 0,
    Data: map[string]any{},
    Msg:  "成功",
  })
  res.Write(byteData)
}
func post22(res http.ResponseWriter, req *http.Request) {
  // 获取参数,post的请求体
  byteData, _ := io.ReadAll(req.Body)
  fmt.Println(string(byteData))
  byteData, _ = json.Marshal(Response{
    Code: 0,
    Data: map[string]any{},
    Msg:  "成功",
  })
  res.Write(byteData)
}

func main() {
  http.HandleFunc("/get33", get22)
  http.HandleFunc("/post33", post22)

  http.ListenAndServe(":8080", nil)
}

gin

使用net/http封装的web框架,提供简洁的api,支持多种数据格式,适用于构建RESTful API、Web应用等场景。

zinx

使用net中tcp模块封装的web框架,特点是TCP长连接和自定义协议,适用于游戏服务器、实时通讯系统等,可扩展性更大。

相关推荐
codeejun2 小时前
每日一Go-73、云原生成本优化 —— 资源限制 & 指标驱动扩容
开发语言·云原生·golang
广州灵眸科技有限公司4 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) Linux虚拟机准备
linux·运维·服务器
Lana学习中5 小时前
【运维杂记】连接不上远程服务器的问题处理
运维·服务器
189228048615 小时前
NV023固态MT29F16T08GWLCEJ9-QBES:C
大数据·服务器·人工智能·科技·缓存
LZZ and MYY7 小时前
RTS 在windows和Linux之间ShareMem
linux·运维·服务器
爱学习的徐徐7 小时前
Linux 基础IO
linux·服务器
蛋蛋的学习记录7 小时前
C#窗体应用中使用EasyModbusCore通讯
服务器·c#·tcp
zt1985q7 小时前
本地部署源代码管理解决方案 Bitbucket Data Center 并实现外部访问
运维·服务器·数据库·网络协议·postgresql·源代码管理
禹凕8 小时前
Linux基础——环境
linux·运维·服务器·ubuntu
糖果店的幽灵9 小时前
Claude Code 完全实战指南 - 第四章:Skill 怎么写
java·服务器·前端