免费搭建手机网站源码,抖音黑科技引流推广神器,电子商务类网站有哪些,windows优化大师下载安装一#xff0c;Gin介绍
Gin是一个 Go (Golang) 编写的轻量级 http web 框架#xff0c;运行速度非常快#xff0c;如果你是性能和高效的追求者#xff0c;我们推荐你使用Gin框架。
Gin最擅长的就是Api接口的高并发#xff0c;如果项目的规模不大#xff0c;业务相对简单…一Gin介绍
Gin是一个 Go (Golang) 编写的轻量级 http web 框架运行速度非常快如果你是性能和高效的追求者我们推荐你使用Gin框架。
Gin最擅长的就是Api接口的高并发如果项目的规模不大业务相对简单这个时候我们也推荐你使用 Gin.
当某个接口的性能遭到较大挑战的时候这个还是可以考虑使用Gin重写接口。
Gin 也是一个流行的 golang Web 框架Github Start 量已经超过了50k.
Gin的官网
Gin的官网
https://gin-gonic.com/zh-cn/
Gin Github地址
https://github.com/gin-gonic/gin
二Gin环境搭建
要安装 Gin 软件包需要先安装 Go并设置 Go 工作区。
1下载并安装 gin:
go get -u github.com/gin-gonic/ginhttps://github.com/gin-gonic/gin
go install github.com/gin-gonic/ginlatest
go mod init gindemo01
go mod init Administrator
go mod tidy
go get -u github.com/gin-gonic/gin
2将 gin 引入到代码中:
import github.com/gin-gonic/gin
3(可选) 如果使用诸如 http.StatusOK之类的常量则需要引入 net/http 包
import net/http
4新建 Main.go 配置路由
package main
import main() {// 创建一个默认的路由引擎r : gin.Default()// 配置路由r.GET(/,func(c *gin.Context) {c.JSON(200,gin.H{ // c.JSON返回JSON格式的数据message:Hello world!,})})// 启动 HTTP 服务默认在 0.0.0.0:8080 启动服务r.Run()
}
5运行你的项目
go run main.goPS G:\GO\gin\gindemo01 go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in debug mode. Switch to release mode in production.- using env: export GIN_MODErelease- using code: gin.SetMode(gin.ReleaseMode)[GIN-debug] GET / -- main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2025/01/06 - 10:29:16 | 200 | 548.6µs | ::1 | GET /
[GIN] 2025/01/06 - 10:29:16 | 404 | 0s | ::1 | GET /favicon.ico
[GIN] 2025/01/06 - 10:32:48 | 404 | 0s | ::1 | GET /new
// 改完代码以后要重新运行
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2025/01/06 - 10:34:50 | 200 | 0s | ::1 | GET /
[GIN] 2025/01/06 - 10:34:53 | 200 | 0s | ::1 | GET /new
6要改变默认启动的端口
r.Run(:9000)
如果 go get 失败请参考
http://bbs.itying.com/topic/Sed08edee7c0790f8475e276
三golang程序的热加载
所谓热加载就是当我们对代码进行修改时程序能够自动重新加载并执行这在我们开发中是非常便利的可以快速进行代码测试省去了每次手动重新编译
beego中我们可以使用官方给我们提供的bee工具来热加载项目但是gin中并没有官方提供的热加载工具这个时候我们要实现热加载就可以借助第三方的工具。
工具1 (推荐)
https://github.com/gravityblast/freshgo install github.com/gravityblast/freshlatestgo get github.com/pilu/freshG:/GO/gin_demofresh
工具2
https://github.com/codegangsta/gingo get -u github.com/codegangsta/ginG:/GO/gin_demogin run main.go
四Gin框架中的路由
4.1路由概述
路由 (Routing) 是由一个 URI (或者叫路径) 和一个特定的 HTTP方法(GET,POST等)组成的涉及到应用如何响应客户端对某个网站节点的访问。
RESTful API 是目前比较成熟的一套互联网应用程序的API设计理论所以我们设计我们的路由的时候建议参考 RESTful API 指南。
在 RESTful 架构中每个网址代表一种资源不同的请求方式表示执行不同的操作
GET(SELECT)从服务器取出资源(一项或多项)POST(CREATE)在服务器新建一个资源PUT(UPDATE)在服务器更新资源(客户端提供改变后的完整资源)DELETE(DELETE)从服务器删除资源
4.2简单的路由配置
简单的路由配置(可以通过 postman 测试)
当用GET请求访问一个网址的时候做什么事情
路由配置实践
package mainimport (net/httpgithub.com/gin-gonic/gin
)func main() {// 创建一个默认的路由引擎r : gin.Default()// 配置路由 绑定路由规则执行的函数r.GET(/, func(c *gin.Context) {c.String(http.StatusOK, 值:%v, 您好gin)})r.GET(/news, func(c *gin.Context) {c.String(http.StatusOK, 我是新闻页面 111)})// r.GET(/about, func(c *gin.Context) {// c.String(200, 我是关于页面 222)// })// r.GET(/login, func(c *gin.Context) {// c.String(200, 这是一个登录页面)// })// r.GET(/register, func(c *gin.Context) {// c.String(200, 这是一个注册页面)// })// r.GET(/user/:name, func(c *gin.Context) {// name : c.Param(name)// c.String(200, 这是一个用户页面 %s, name)// })// r.GET(/user/:name/:age, func(c *gin.Context) {// name : c.Param(name)// age : c.Param(age)// c.String(200, 这是一个用户页面 %s %s, name, age)// })// r.GET(/user/:name/*action, func(c *gin.Context) {// name : c.Param(name)// action : c.Param(action)// c.String(200, 这是一个用户页面 %s %s, name, action)// })// r.GET(/favicon.ico, func(c *gin.Context) {// c.String(200, 这是一个图标页面)// })// r.GET(/favicon.ico, func(c *gin.Context) {// c.String(200, 这是一个图标页面2)// })r.POST(/add, func(c *gin.Context) {c.String(http.StatusOK, 这是一个post请求 主要用于增加数据)// c.JSON(200, gin.H{// name: 小明,// })})r.PUT(/edit, func(c *gin.Context) {c.String(http.StatusOK, 这是一个put请求 主要用于编辑数据)})r.DELETE(/delete, func(c *gin.Context) {c.String(http.StatusOK, 这是一个delete请求 主要用于删除数据)})//启动一个web服务 监听并在 0.0.0.0:8080 上r.Run() // listen and serve on 0.0.0.0:8080 (for windows localhost:8080)//r.Run(:8000) // 指定端口启动服务
}4.3c.String() c.JSON() c.JSONP() c.XML() c.HTML()
返回一个字符串
r.GET(/news,fnuc(c *gin.Context) {aid : c.Query(aid)c.String(200,aid%s,aid)
})
返回一个 JSON 数据
func main() {r : gin.Default()// gin.H 是 map[string]interface{}的缩写r.GET(/someJSON, func(c *gin.Context) {// 方式一: 自己拼接JSONc.JSON(http.StatusOK,gin.H{message: Hello world!})})r.GET(/moreJSON,func(c *gin.Context) {// 方式二使用结构体var msg struct {Name string json:user}})
}
新建项目实践