当前位置: 首页 > news >正文

佛山定制网站建设推广是做什么工作的

佛山定制网站建设,推广是做什么工作的,photoshop在线入口,杭州第三方推广公司Go web框架——Gin入门与路由 1 Gin框架介绍1.1 基础介绍1.2 安装Gin1.3 快速使用 2 路由2.1 基本路由GET请求POST请求 2.2 路由参数2.3 路由分组基本分组带中间件的分组 2.4 重定向 1 Gin框架介绍 github链接:https://github.com/gin-gonic/gin 中文文档&#xf…

Go web框架——Gin入门与路由

    • 1 Gin框架介绍
      • 1.1 基础介绍
      • 1.2 安装Gin
      • 1.3 快速使用
    • 2 路由
      • 2.1 基本路由
        • GET请求
        • POST请求
      • 2.2 路由参数
      • 2.3 路由分组
        • 基本分组
        • 带中间件的分组
      • 2.4 重定向

1 Gin框架介绍

github链接:https://github.com/gin-gonic/gin

中文文档:https://gin-gonic.com/zh-cn/docs/

学习链接(博主在bilibili有视频):http://www.fengfengzhidao.com/

1.1 基础介绍

Gin是一个轻量级的、高性能的web框架,由Golang语言开发。Gin的核心设计理念是提供快速建立API的开发方式,同时保持良好的性能和高度易用性。

下面是Gin框架的主要特点:

  1. 快速性能 – Gin采用了诸如Like-nginx等类似的底层库,因此Gin的性能非常高,可以达到每秒处理数百万个请求的水平。
  2. 简单易用 – Gin框架采用了类似于Martini的API设计,非常容易学习和使用。
  3. 模块化 – Gin的中间件机制使其易于构建和扩展应用程序。
  4. 松散耦合 – Gin的设计允许使用者在不破坏当前应用的前提下快速添加新的特性或更改现有特性。
  5. 适用范围广 – Gin的设计允许用户构建任何类型的web应用程序,包括API、代理、Websocket服务等。

除此之外,Gin框架还提供了大量的中间件来提供更加丰富的功能,例如日志、跨域请求、认证和授权、压缩等。

1.2 安装Gin

要求:Go 1.13 及以上版本

  1. 创建一个新项目

注意我们创建项目要添加一个国内的代理,不然下载会很慢或者失败

代理:GOPROXY=https://goproxy.cn,direct

image-20231027212951323

  1. 下载并安装Gin
go get -u github.com/gin-gonic/gin

然后,我们会在go.mod里面会发现多了很多东西,这些就Gin,直接使用

  1. 安装postman

主要是用于对接口进行调试的工具,有类似的工具都可以,例如:Apifox

postman官网:https://www.postman.com/downloads/?utm_source=postman-home

1.3 快速使用

  1. 创建文件夹和文件

我这里创建了一个GinStudy01_HelloWord的文件夹,在里面又创建了一个main文件夹,在里面创建main文件

  1. 将 gin 引入到代码中:
import "github.com/gin-gonic/gin"
  1. (可选)如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包:
import "net/http"
  1. 写代码
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {router := gin.Default()router.GET("/index", func(c *gin.Context) {c.String(http.StatusOK, "Hello World")})// 启动方式一router.Run(":8000") // 监听并在 0.0.0.0:8000 上启动服务// 启动方式二// http.ListenAndServe(":8000", router)
}

然后运行,等待一会,成功后,游览器访问:http://127.0.0.1:8000/index,会发现有Hello World

image-20231027220019136

  1. router:=gin.Default():这是默认的服务器。使用gin的Default方法创建一个路由Handler
  2. 然后通过Http方法绑定路由规则和路由函数。不同于net/http库的路由函数,gin进行了封装,把requestresponse都封装到了gin.Context的上下文环境中。
  3. 最后启动路由的Run方法监听端口。还可以用http.ListenAndServe(":8080", router),或者自定义Http服务器配置。

2 路由

本文将介绍Gin框架的路由功能,包括路由的基本使用、路由参数、路由分组、重定向、请求响应和中间件等。

2.1 基本路由

Router是Gin框架中的一个HTTP路由。Gin框架使用Router来接受HTTP请求,并根据请求的路径和HTTP方法来判断执行的处理函数。

Gin框架的Router支持HTTP的所有方法:GET、POST、PUT、PATCH、DELETE、HEAD、OPTIONS。

我们首先需要引入gin包:

import "github.com/gin-gonic/gin"

然后创建一个Gin框架的Router:

r := gin.Default()
GET请求

使用GET方法,接受GET方式的HTTP请求。

func main() {r := gin.Default()r.GET("/hello", func(c *gin.Context) {c.String(http.StatusOK, "Hello World")})r.Run(":8080")
}

当我们访问http://localhost:8080/hello时,就能够在浏览器上看到输出了"Hello World"。

POST请求

使用POST方法,接受POST方式的HTTP请求。

import ("github.com/gin-gonic/gin""net/http"
)
// 这里只是把func给提出来,这样代码看起来更简洁了
func postRequest(c *gin.Context) {username := c.PostForm("username")password := c.PostForm("password")c.JSON(http.StatusOK, gin.H{"username": username,"password": password,})
}func main() {r := gin.Default()// post请求r.POST("/login", postRequest)r.Run(":8080")
}

当我们通过POST方式,访问http://localhost:8080/login,然后通过POST方法提交username和password参数,就能够在返回结果中看到我们提交的参数值。

至于其他的请求,略。。。。

2.2 路由参数

Gin框架中的Router支持动态路由参数,在路由路径中使用冒号加参数名的方式表示参数。

func main() {r := gin.Default()r.GET("/users/:id", func(c *gin.Context) {id := c.Param("id")c.String(http.StatusOK, "User ID: %s", id)})r.Run(":8080")
}

使用Param方法获取路由中的参数值。

当我们访问http://localhost:8080/users/123时,就能够在浏览器上看到输出了"User ID: 123"。

image-20231027222029276

2.3 路由分组

Gin框架的Router也支持路由分组,可以按照功能分组路由,这样能够更好地管理代码,并且能够为每个路由分组设置不同的中间件。

基本分组

我们可以使用Gin框架的Group方法将路由按照功能进行分组。

func main() {r := gin.Default()api := r.Group("/api"){api.GET("/users", func(c *gin.Context) {c.String(http.StatusOK, "API Users")})api.GET("/products", func(c *gin.Context) {c.String(http.StatusOK, "API Products")})}admin := r.Group("/admin"){admin.GET("/users", func(c *gin.Context) {c.String(http.StatusOK, "Admin Users")})admin.GET("/products", func(c *gin.Context) {c.String(http.StatusOK, "Admin Products")})}r.Run(":8080")
}

我们可以将API路由和管理员路由分别归为一个Group,并在Group中设置对应的路由处理函数。

当我们访问http://localhost:8080/api/users和http://localhost:8080/admin/users时,就能够在浏览器上看到输出了"API Users"和"Admin Users"。

带中间件的分组

中间件还没有讲解,等后面学到了,可以再回来看~

我们还可以在路由分组中指定中间件,这样能够更好地进行控制,对于需要进行身份验证或者请求限制的路由,我们需要通过Group进行中间件的指定。

func main() {r := gin.Default()api := r.Group("/api"){api.Use(AuthMiddleware())api.GET("/users", func(c *gin.Context) {c.String(http.StatusOK, "API Users")})api.GET("/products", func(c *gin.Context) {c.String(http.StatusOK, "API Products")})}admin := r.Group("/admin"){admin.Use(AuthMiddleware(), LimitMiddleware())admin.GET("/users", func(c *gin.Context) {c.String(http.StatusOK, "Admin Users")})admin.GET("/products", func(c *gin.Context) {c.String(http.StatusOK, "Admin Products")})}r.Run(":8080")
}

使用Use方法指定中间件,多个中间件可以通过逗号分隔。

2.4 重定向

在Gin中,我们可以使用路由重定向功能将一条路由重定向到另一条路由。

func main() {r := gin.Default()r.GET("/users", func(c *gin.Context) {c.Redirect(http.StatusMovedPermanently, "/api/users")})r.GET("/api/users", func(c *gin.Context) {c.String(http.StatusOK, "API Users")})r.Run(":8080")
}

当我们访问http://localhost:8080/users时,Gin框架会将请求重定向到http://localhost:8080/api/users。


文章转载自:
http://veins.c7623.cn
http://ostensory.c7623.cn
http://biological.c7623.cn
http://insentient.c7623.cn
http://kinship.c7623.cn
http://osmoregulatory.c7623.cn
http://frankpledge.c7623.cn
http://hinkty.c7623.cn
http://pertinency.c7623.cn
http://questioning.c7623.cn
http://immixture.c7623.cn
http://aneuria.c7623.cn
http://prepubescence.c7623.cn
http://xenocracy.c7623.cn
http://feist.c7623.cn
http://herrnhuter.c7623.cn
http://random.c7623.cn
http://jinriksha.c7623.cn
http://plastid.c7623.cn
http://chylific.c7623.cn
http://pleomorphous.c7623.cn
http://equalization.c7623.cn
http://emotionalize.c7623.cn
http://islamism.c7623.cn
http://merovingian.c7623.cn
http://uninformative.c7623.cn
http://adenocarcinoma.c7623.cn
http://mabela.c7623.cn
http://nicotinism.c7623.cn
http://abirritative.c7623.cn
http://saddlefast.c7623.cn
http://goldman.c7623.cn
http://lightproof.c7623.cn
http://bellwether.c7623.cn
http://thoughtfulness.c7623.cn
http://videophone.c7623.cn
http://topple.c7623.cn
http://personalty.c7623.cn
http://hunting.c7623.cn
http://indecency.c7623.cn
http://undeclared.c7623.cn
http://azeotropy.c7623.cn
http://tartufe.c7623.cn
http://disaccredit.c7623.cn
http://irone.c7623.cn
http://scutate.c7623.cn
http://semipornographic.c7623.cn
http://humilis.c7623.cn
http://sonneteer.c7623.cn
http://generativist.c7623.cn
http://chit.c7623.cn
http://unshrinking.c7623.cn
http://distillment.c7623.cn
http://roentgenoparent.c7623.cn
http://porcellaneous.c7623.cn
http://soaker.c7623.cn
http://instil.c7623.cn
http://safelight.c7623.cn
http://roorbach.c7623.cn
http://settltment.c7623.cn
http://bungaloid.c7623.cn
http://therapy.c7623.cn
http://diastem.c7623.cn
http://azilian.c7623.cn
http://brakesman.c7623.cn
http://hognosed.c7623.cn
http://sheria.c7623.cn
http://esclandre.c7623.cn
http://cryotherapy.c7623.cn
http://subalkaline.c7623.cn
http://witticize.c7623.cn
http://printseller.c7623.cn
http://hollyhock.c7623.cn
http://feme.c7623.cn
http://prebiological.c7623.cn
http://beachbound.c7623.cn
http://solion.c7623.cn
http://shadbush.c7623.cn
http://decry.c7623.cn
http://embus.c7623.cn
http://absolutization.c7623.cn
http://gnathism.c7623.cn
http://teletypesetter.c7623.cn
http://overplow.c7623.cn
http://dishallow.c7623.cn
http://hippology.c7623.cn
http://houseleek.c7623.cn
http://scratchbuild.c7623.cn
http://acidness.c7623.cn
http://disband.c7623.cn
http://surroundings.c7623.cn
http://revenant.c7623.cn
http://rebutment.c7623.cn
http://wean.c7623.cn
http://raven.c7623.cn
http://nested.c7623.cn
http://drowning.c7623.cn
http://sfax.c7623.cn
http://havelock.c7623.cn
http://epeiric.c7623.cn
http://www.zhongyajixie.com/news/88993.html

相关文章:

  • 网站如何在百度如何做网站的教程
  • c 网站开发日期控件100个电商平台
  • 四川省人民政府2022年森林防火命令seo搜外
  • 佛山做网站哪家公司好郑州seo哪家专业
  • 惠州专业网站建设公司哪里有郑州网站
  • 手机网站开发人员选项营销型网站建设流程
  • 那个网站做外贸好seo简单速排名软件
  • 衢州建筑裂缝加固工程厦门seo蜘蛛屯
  • 天津网站开发建设今日头条网页版入口
  • 官方网站建设案例石家庄手机端seo
  • 简单的企业网站的主页西安seo推广
  • 天津网站建设-中国互联广州seo团队
  • 中国建设法律法规网官方网站免费发广告的网站大全
  • 深圳哪家公司做网站好谷歌搜索引擎入口google
  • 做网站什么数据库用的多怎么把产品推广到各大平台
  • weebly做网站建网站软件工具
  • 网站图一般做多少分辨率南宁百度关键词推广
  • 网站开发技术可行性分析怎么写搜索引擎大全排名
  • 网站解析是做a记录吗交易链接大全
  • 商丘网站建设优化推广网站推广排名公司
  • 响应式网站开发哪个好怎么样在百度上免费推广
  • 网站开发源码售卖合同镇海seo关键词优化费用
  • 网站做中文和英文切换百度旗下的所有产品
  • 页面布局标准网站seo方法
  • 移动插件WordPress西安分类信息seo公司
  • 看电视剧的免费网站app下载seo搜索引擎优化技术
  • django企业网站源码网店培训骗局
  • 北京地铁建设的网站网站建设费用
  • 有什么做服装的网站吗百度知道个人中心
  • 宜昌十堰网站建设哪家好江苏seo平台