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

怎样看网站的建设时间网站ftp查询

怎样看网站的建设时间,网站ftp查询,百度推广没有效果怎么办,安卓市场下载appGo语言GIN框架安装与入门 文章目录 Go语言GIN框架安装与入门1. 创建配置环境2. 配置环境3. 下载最新版本Gin4. 编写第一个接口5. 静态页面和资源文件加载6. 各种传参方式6.1 URL传参6.2 路由形式传参6.3 前端给后端传递JSON格式6.4 表单形式传参 7. 路由和路由组8. 项目代码mai…Go语言GIN框架安装与入门 文章目录 Go语言GIN框架安装与入门1. 创建配置环境2. 配置环境3. 下载最新版本Gin4. 编写第一个接口5. 静态页面和资源文件加载6. 各种传参方式6.1 URL传参6.2 路由形式传参6.3 前端给后端传递JSON格式6.4 表单形式传参 7. 路由和路由组8. 项目代码main.go9. 总结 之前学习了一周的GO语言学会了GO语言基础现在尝试使用GO语言最火的框架GIN来写一些简单的接口。看了B站狂神说的GIN入门视频基本明白如何写接口了下面记录一下基本的步骤。 1. 创建配置环境 我们使用Goland创建第一个新的开发环境这里只要在windows下面安装好Go语言Goroot都能自动识别。 新的项目也就只有1个go.mod的文件用来表明项目中使用到的第三方库。 2. 配置环境 我们使用第三方库是需要从github下载的但是github会经常连不上所以我们就需要先配置第三方的代理地址。我们再Settings-Go-Go Modules-Environment下面配上代理地址。 GOPROXYhttps://goproxy.cn,direct3. 下载最新版本Gin 在IDE里面的Terminal下面安装Gin框架使用下面的命令安装Gin安装完成以后go.mod下面require就会自动添加依赖。 go get -u github.com/gin-gonic/gin4. 编写第一个接口 创建main.go文件然后编写以下代码这里定义了一个/hello的路由。 package mainimport github.com/gin-gonic/ginfunc main() {ginServer : gin.Default()ginServer.GET(/hello, func(context *gin.Context) {context.JSON(200, gin.H{msg: hello world})})ginServer.Run(:8888)}编译运行通过浏览器访问就可以输出JSON。 5. 静态页面和资源文件加载 使用下面代码加入项目下面的静态页面(HTML文件)以及动态资源(JS)。 // 加载静态页面ginSever.LoadHTMLGlob(templates/*)// 加载资源文件ginSever.Static(/static, ./static) 这是项目的资源文件列表 其中index.html文件如下 !DOCTYPE html html langen headmeta charsetUTF-8title我的第一个GO web页面/titlelink relstylesheet href/static/css/style.cssscript src/static/js/common.js/script /head bodyh1谢谢大家支持/h1获取后端的数据为 {{.msg}}form action/user/add methodpostpusername: input typetext nameusername/pppassword: input typetext namepassword/pbutton typesubmit 提 交 /button /form/body /html接着就可以响应一个页面给前端了。 // 响应一个页面给前端ginSever.GET(/index, func(context *gin.Context) {context.HTML(http.StatusOK, index.html, gin.H{msg: 这是go后台传递来的数据,})})6. 各种传参方式 6.1 URL传参 在后端获取URL传递来的参数。 // 传参方式//http://localhost:8082/user/info?userid123usernamedfaginSever.GET(/user/info, myHandler(), func(context *gin.Context) {// 取出中间件中的值usersession : context.MustGet(usersession).(string)log.Println(, usersession)userid : context.Query(userid)username : context.Query(username)context.JSON(http.StatusOK, gin.H{userid: userid,username: username,})}) 其中上面多加了一个中间键就是接口代码运行之前执行的代码myHandler的定义如下 // go自定义中间件 func myHandler() gin.HandlerFunc {return func(context *gin.Context) {// 设置值后续可以拿到context.Set(usersession, userid-1)context.Next() // 放行} }6.2 路由形式传参 // http://localhost:8082/user/info/123/dfaginSever.GET(/user/info/:userid/:username, func(context *gin.Context) {userid : context.Param(userid)username : context.Param(username)context.JSON(http.StatusOK, gin.H{userid: userid,username: username,})})6.3 前端给后端传递JSON格式 // 前端给后端传递jsonginSever.POST(/json, func(context *gin.Context) {// request.bodydata, _ : context.GetRawData()var m map[string]interface{}_ json.Unmarshal(data, m)context.JSON(http.StatusOK, m)})6.4 表单形式传参 ginSever.POST(/user/add, func(context *gin.Context) {username : context.PostForm(username)password : context.PostForm(password)context.JSON(http.StatusOK, gin.H{msg: ok,username: username,password: password,})})7. 路由和路由组 // 路由ginSever.GET(/test, func(context *gin.Context) {context.Redirect(http.StatusMovedPermanently, https://www.baidu.com)})// 404ginSever.NoRoute(func(context *gin.Context) {context.HTML(http.StatusNotFound, 404.html, nil)})// 路由组userGroup : ginSever.Group(/user){userGroup.GET(/add)userGroup.POST(/login)userGroup.POST(/logout)}orderGroup : ginSever.Group(/order){orderGroup.GET(/add)orderGroup.DELETE(delete)}8. 项目代码main.go package mainimport (encoding/jsongithub.com/gin-gonic/ginlognet/http )// go自定义中间件 func myHandler() gin.HandlerFunc {return func(context *gin.Context) {// 设置值后续可以拿到context.Set(usersession, userid-1)context.Next() // 放行} }func main() {// 创建一个服务ginSever : gin.Default()//ginSever.Use(favicon.New(./icon.png))// 加载静态页面ginSever.LoadHTMLGlob(templates/*)// 加载资源文件ginSever.Static(/static, ./static)//ginSever.GET(/hello, func(context *gin.Context) {// context.JSON(200, gin.H{msg: hello world})//})//ginSever.POST(/user, func(c *gin.Context) {// c.JSON(200, gin.H{msg: post,user})//})//ginSever.PUT(/user)//ginSever.DELETE(/user)// 响应一个页面给前端ginSever.GET(/index, func(context *gin.Context) {context.HTML(http.StatusOK, index.html, gin.H{msg: 这是go后台传递来的数据,})})// 传参方式//http://localhost:8082/user/info?userid123usernamedfaginSever.GET(/user/info, myHandler(), func(context *gin.Context) {// 取出中间件中的值usersession : context.MustGet(usersession).(string)log.Println(, usersession)userid : context.Query(userid)username : context.Query(username)context.JSON(http.StatusOK, gin.H{userid: userid,username: username,})})// http://localhost:8082/user/info/123/dfaginSever.GET(/user/info/:userid/:username, func(context *gin.Context) {userid : context.Param(userid)username : context.Param(username)context.JSON(http.StatusOK, gin.H{userid: userid,username: username,})})// 前端给后端传递jsonginSever.POST(/json, func(context *gin.Context) {// request.bodydata, _ : context.GetRawData()var m map[string]interface{}_ json.Unmarshal(data, m)context.JSON(http.StatusOK, m)})// 表单ginSever.POST(/user/add, func(context *gin.Context) {username : context.PostForm(username)password : context.PostForm(password)context.JSON(http.StatusOK, gin.H{msg: ok,username: username,password: password,})})// 路由ginSever.GET(/test, func(context *gin.Context) {context.Redirect(http.StatusMovedPermanently, https://www.baidu.com)})// 404ginSever.NoRoute(func(context *gin.Context) {context.HTML(http.StatusNotFound, 404.html, nil)})// 路由组userGroup : ginSever.Group(/user){userGroup.GET(/add)userGroup.POST(/login)userGroup.POST(/logout)}orderGroup : ginSever.Group(/order){orderGroup.GET(/add)orderGroup.DELETE(delete)}// 端口ginSever.Run(:8888)} 9. 总结 以上就是Gin入门的所有内容了大家觉得还有帮助欢迎点赞收藏哦。
http://www.hkea.cn/news/14449575/

相关文章:

  • 营销网站建设企划案例12380网站建设情况汇报
  • 阳泉住房和城乡建设厅网站百度seo发帖推广
  • 网站首页做多大分辨率wordpress 下雪插件
  • 视频网站开发视频教程北京网站建设百度排名
  • 宁波公司网站开发网络销售平台推广
  • 做普通网站公司吗四川建设网中标候选人公示
  • 做全景效果图的网站网站建设公司怎么找业务
  • 搜搜提交网站怎样弄微信公众号
  • 网站如何做3d产品展示丝芭传媒有限公司
  • 程序员为什么35岁就不能干?合肥市网站优化
  • 网站开发的软 硬件环境标准请求做女朋友的网站源码
  • 怎么自己做网站的推广杭州编程培训机构排名
  • 重庆万州网站建设多少钱iis 一个网站多个应用程序
  • 网站维护的基本内容有哪些信息展示网站系统
  • 天津市城乡建设部网站首页免费企业名录数据
  • 韶关最新消息做网站分为竞价和优化
  • 网站建设服务哪家好 价格多少钱网站规划建设实训
  • 个人电脑做网站服务器教程泰安人才招聘网最新招聘2023
  • 娄底网站建设79ld吴桥网站建设价格
  • 网页设计网站开发需要什么软件网站建设开发全包
  • 什么网站有教做详情页6wordpress
  • 网站开发 加密保护深圳网站开发专业
  • 做平面免费接单网站我想建设网站
  • 宿松网站建设wordpress怎么多用户
  • 网站建设给客户看的ppt模板中国江西网官方网站
  • 能做网站的网站免费网页建设
  • saas建站 彩页用家里的电脑做网站服务器
  • 建设门户网站需要注意什么意思中文 wordpress 主题
  • 自己怎么创网站微信网页版平板
  • 网站建设和电子商务的关系网站如何防止被攻击