Gin框架学习笔记
侧边栏壁纸
  • 累计撰写 42 篇文章
  • 累计收到 82 条评论
  • 今日撰写 0 篇文章

Gin框架学习笔记
2022-07-13 / 0 评论 / 692 阅读 / 0 点赞

Gin框架学习笔记

绎泽
2022-07-13 / 0 评论 / 692 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年07月13日,已超过745天没有更新,若内容或图片失效,请留言反馈。

Gin是一个基于go语言编写的web框架,因为Gin的路由库基于httprouter开发的,性能非常好,支持Restful api规范

安装

go get -u github.com/gin-gonic/gin

第一个demo

package main
import "github.com/gin-gonic/gin"
import "net/http"
func main() {
g := gin.Default()
g.GET("/", func(c *gin.Context) {
    c.String(http.StatusOK, "hallo word")
})
g.Run()
}

go run main.go

g.Run()是将应用部署到本地服务器上,默认端口为8080,可设置端口,g.Run(":2333")

路由

r.GET("/test/:name", func(c *gin.Context) {
    name := c.Param("name")
    c.String(http.StatusOK, name)
})
g.Run(":6666")

127.0.0.1:6666/test

如果没有传递参数将会输出DefaultQuery的默认参数test

传递参数后 127.0.0.1:6666/test?name=word

POST请求

index.html

<input type="text" name="user" placeholder="name">
<input type="password" name="pass" placeholder="pass">
<input type="submit" value="提交">

main.go

r.POST("/form", func(c *gin.Context) {
    types := c.DefaultPostForm("type", "post")
    user := c.PostForm("user")
    pass := c.PostForm("pass")
    c.String(http.StatusOK, fmt.Sprintf("user:%s,pass:%s,type:%s", name, pass, types))
})
r.Run()
本文共 88 个字数,平均阅读时长 ≈ 1分钟
广告
0

海报

正在生成.....

评论 (0)

取消

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /www/wwwroot/blog/usr/themes/Joe-master/public/footer.php on line 25

Warning: file_get_contents(): Failed to enable crypto in /www/wwwroot/blog/usr/themes/Joe-master/public/footer.php on line 25

Warning: file_get_contents(https://api.vore.top/api/IPdata?ip=154.201.91.42): failed to open stream: operation failed in /www/wwwroot/blog/usr/themes/Joe-master/public/footer.php on line 25