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

相关推荐
xuanzdhc1 小时前
Linux 基础IO
linux·运维·服务器
bantinghy2 小时前
Linux进程单例模式运行
linux·服务器·单例模式
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器
忘了ʷºᵇₐ9 天前
Linux系统能ping通ip但无法ping通域名的解决方法
linux·服务器·tcp/ip
weixin_425023009 天前
Spring Boot使用MCP服务器
服务器·spring boot·后端·spring ai·mcp
qq_200337399 天前
python输出值,写入test.yaml文件
java·服务器·前端
成工小白9 天前
【Linux】文件系统
linux·运维·服务器
PHP武器库9 天前
[Hestia]开源网络服务器控制面板,快速、可靠、开源
运维·服务器·网络·开源·php
(:满天星:)9 天前
Redis哨兵模式深度解析与实战部署
linux·服务器·网络·数据库·redis·缓存·centos