搜 索

gin从入门到放弃

  • 209阅读
  • 2022年05月25日
  • 0评论
首页 / Go/Python / 正文

简介

作为入门使用go写项目,我觉得gin是一个很推荐的选择,gin是一个轻量级的web框架,go编写,使用简单上手快、性能比较好,在微服务中使用gin来提升性能非常不错。

使用

gin初体验

新建空项目studygin
go env -w GO111MODULE=on # 开启mod
go mod init studygin #初始化mod,创建go.mod
go get -u github.com/gin-gonic/gin
curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go
go run main.go
这个时候打开http://localhost:8080/ping显示内容pong就表示ok了

html模板:

router.LoadHTMLGlob("templates/*")
c.HTML(https.StaatusOK, "index.tmpl", gin.H{"title": "Main website"})

他的html模板和django的非常像{{val}} {{define "header.tmpl"}}{{end}}
由于前后端分离,这块估计很少使用到,跳过

中间件

路由规则

 // get 请求, 路由参数
 // :id或*id,层级区别
  r.GET("/users/:id", func(c *gin.Context) {
    // 拿到路由中的 id 值
    id := c.Param("id")
    // 响应值
    c.String(200, "The user id is  %s", id)
  })

数据库

日志打印

CORS

gorm

网络问题:

由于国内的网络问题,导致部分服务无法访问,需要特殊处理
githubusercontent访问不了的问题:
配置hosts
185.199.109.133 raw.githubusercontent.com
proxy.golang.org timeout问题:
go env -w GOPROXY=https://goproxy.cn

参考

Gin官方文档 Gin-demo

评论区
暂无评论
avatar