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

相关推荐
凌寒1126 分钟前
minio预签名访问地址经过Nginx代理后返回403问题
服务器·网络·nginx
广然37 分钟前
EVE-NG 镜像管理工具 1.1 Web 版本正式发布!
运维·服务器·前端
祁鱼鱼鱼鱼鱼41 分钟前
DNS 笔记记录
运维·服务器·网络
tod1131 小时前
Makefile进阶(上)
linux·运维·服务器·windows·makefile·进程
Data_Journal1 小时前
【无标题】
大数据·服务器·前端·数据库·人工智能
豆是浪个1 小时前
Linux(Centos 7.6)命令详解:top
linux·运维·服务器
_叶小格_2 小时前
cp、scp、rsync命令详解
linux·运维·服务器·ubuntu·centos·运维开发
凯子坚持 c3 小时前
Qt常用控件指南(7)
服务器·数据库·qt
mango_mangojuice3 小时前
Linux学习笔记 1.19
linux·服务器·数据库·笔记·学习
i建模3 小时前
linux断点续传下载文件
linux·运维·服务器