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长连接和自定义协议,适用于游戏服务器、实时通讯系统等,可扩展性更大。

相关推荐
m0_485614675 分钟前
Linux-容器基础2
linux·运维·服务器
于齐龙7 分钟前
2025年12月19日 - 操作系统
运维·服务器
QT 小鲜肉13 分钟前
【Linux命令大全】001.文件管理之mattrib命令(实操篇)
linux·运维·服务器·chrome·笔记
molaifeng14 分钟前
像搭积木一样理解 Golang AST
开发语言·后端·golang
杨了个杨898216 分钟前
日志服务部署
运维·服务器
鸠摩智首席音效师38 分钟前
如何在 Linux 上自动清理 Journalctl 日志 ?
linux·运维·服务器
鸠摩智首席音效师2 小时前
如何在 Linux 下以 www-data 用户运行 Crontab ?
linux·运维·服务器
不染尘.2 小时前
应用层之WWW
服务器·javascript·css·网络·网络协议·计算机网络·html
iCxhust2 小时前
linux /etc 目录 etc是什么缩写
linux·运维·服务器·php
QT 小鲜肉2 小时前
【Linux命令大全】001.文件管理之mc命令(实操篇)
linux·运维·服务器·前端·笔记