这是我参与「第五届青训营 」伴学笔记创作活动的第 3 天
简介
- Gin是一个golang的微框架,封装比较优雅,API友好,源码注释比较明确,具有快速灵活,容错方便等特点
- 对于golang而言,web框架的依赖要远比Python,Java之类的要小。自身的
net/http足够简单,性能也非常不错 - 借助框架开发,不仅可以省去很多常用的封装带来的时间,也有助于团队的编码风格和形成规范
网络服务demo
hello world
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
//1.创建路由
r := gin.Default()
//2.绑定路由规则,执行的函数
//gin.Context,封装了request和response
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello world!")
})
//3.监听端口,默认在8000
r.Run("127.0.0.1:8000")
}
运行时问题解析: 在运行过程中出现了两次问题:
- 运行失败,显示端口已被占用
- 打开页面后显示404 page not found
解决方案:
- 由于之前运行其他程序时已经占用了8080端口,所以现在占用8080端口失败,将8080改为8000后成功运行
- 路径设置错误,之前在路由路径的设置中多写了一个目录,导致出现了404,改过来后成功输出
输出结果如下:
其他测试:将输出由string格式改为json格式后仍能正常输出,输出结果如下:
其他请求测试
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
//创建gin实例
r := gin.Default()
//设置路由:请求方式,路径,处理函数
//gin.Context,封装了request和response
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "这是一个Get ping请求")
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
//Post请求:http://127.0.0.1:8000/login 表示像服务器发送登陆请求
r.POST("/login", func(c *gin.Context) {
c.String(http.StatusOK, "登陆成功,这是一个Post请求")
//获取参数
username := c.PostForm("username")
password := c.PostForm("password")
//返回json数据
c.JSON(http.StatusOK, gin.H{
"username": username,
"password": password,
})
})
//Put请求:http://127.0.0.1:8000/put 表示向服务器发送修改请求
r.PUT("/put", func(c *gin.Context) {
c.String(http.StatusOK, "修改成功,这是一个Put请求")
//获取参数
username := c.PostForm("username")
password := c.PostForm("password")
c.JSON(http.StatusOK, gin.H{
"username": username,
"password": password,
})
})
//Delete请求:http://127.0.0.1:8000/delete 表示向服务器发送删除请求
r.DELETE("/delete", func(c *gin.Context) {
c.String(http.StatusOK, "修改成功,这是一个Put请求")
//获取参数
username := c.PostForm("username")
password := c.PostForm("password")
c.JSON(http.StatusOK, gin.H{
"username": username,
"password": password,
})
})
//3.监听端口,默认在8080
r.Run("127.0.0.1:8000")
}
请求测试:
请求测试结果:
这里是利用postman插件测试的,可以发送各种不同的请求,如果只用网址的url请求,类型只能是get请求,如下:
加载yaml和xml
这里的加载与上面的类似,只是需要以特定的格式展示。这里是利用自带的函数将json格式的数据直接转为了xml格式和yaml格式数据。
主体代码如下:
type User struct {
Username string `json:"用户名"`
Password string `json:"密码"`
}
func main() {
r := gin.Default()
//相应的xml数据:http://127.0.0.1:8001/xml
r.GET("/xml", func(c *gin.Context) {
a := &User{
Username: "admin",
Password: "123456",
}
//返回xml数据
c.XML(http.StatusOK, a)
})
r.GET("/yaml", func(c *gin.Context) {
a := &User{
Username: "admin",
Password: "123456",
}
c.YAML(http.StatusOK, a)
})
r.Run("127.0.0.1:8001")
}
测试结果:
html网页展示
在项目中增加html文件模板,并将设置的数据导入模板中。其中代码项目的结构如下:
主体体代码如下:
func main() {
r := gin.Default()
//html:http://127.0.0.1:8001/
r.LoadHTMLGlob("templates/*") //表示加载此文件下的所有文件
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Main website",
"t": "xiaoming",
})
})
r.GET("/news", func(c *gin.Context) {
n := New{
Title: "new title",
Content: "new content",
}
c.HTML(http.StatusOK, "news.html", gin.H{
"title": "New website",
"news": n,
"t": "t",
})
})
r.Run("127.0.0.1:8000")
}
运行结果:
总结
本文是参照github.com/cubxxw/cs-a… 文章进行学习gin框架和应用的学习笔记,这篇文章的应用范例简单易懂,也让我对gin和网络框架有了更深的了解。