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

建设网站的程序海外游戏推广平台

建设网站的程序,海外游戏推广平台,高端网站建设哪家好,亚马逊雨林的资料一.Gin 介绍Gin 是一个 Go (Golang) 编写的轻量级 http web 框架,运行速度非常快,如果你是性能和高效的追求者,推荐你使用 Gin 框架.Gin 最擅长的就是 Api 接口的高并发,如果项目的规模不大,业务相对简单,这…

一.Gin 介绍

Gin 是一个 Go (Golang) 编写的轻量级 http web 框架,运行速度非常快,如果你是性能和高效的追求者,推荐你使用 Gin 框架.
Gin 最擅长的就是 Api 接口的高并发,如果项目的规模不大,业务相对简单,这个时候
也推荐使用 Gin.
当某个接口的性能遭到较大挑战的时候,这个还是可以考虑使用 Gin 重写接口.
Gin 也是一个流行的 golang Web 框架,Github Strat 量已经超过了 50k.
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/gin

由于网络原因国内部分用户可能没法直接下载第三方包,go get 失败,Golang Gin中没法下载第三方包解决办法如下:

//打开终端并执行
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

2.将 gin 引入到代码中

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

3. 如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包

import "net/http"

4.新建main.go配置路由

package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {
// 创建gin的默认路由引擎
r := gin.Default()
// 配置路由
r.GET("/", func(c *gin.Context) {c.JSON(200, gin.H{ // c.JSON:返回 JSON 格式的数据"message": "Hello world!", })})// 浏览器访问请求127.0.0.1:8080/ping路由时,调用回调函数
r.GET("/ping", func(c *gin.Context) {//浏览器输出//c.JSON(200, gin.H{//    "message": "pong",//})c.String(http.StatusOK, "值:%v", "你好")})//监听并在 0.0.0.0:8080 上启动服务(启动一个web服务)
r.Run()
// 监听并在 0.0.0.0:8000 上启动服务(启动一个web服务)
//r.Run("8000") 
}

5.运行项目

go run main.go

6.要改变默认启动的端口

r.Run(":9000")

三.golang程序的热加载

所谓热加载就是当对代码进行修改时,程序能够自动重新加载并执行,这在开发中
是非常便利的,可以快速进行代码测试,省去了每次手动重新编译,beego 中可以使用官方给提供bee工具来热加载项目,但是 gin 中并没有官方提供的热加载工具,这个时候要实现热加载就可以借助第三方的工具

工具 1(推荐):https://github.com/gravityblast/fresh

//进入终端执行
go get github.com/pilu/fresh
//然后运行命令
fresh//或者进入终端执行
go install github.com/pilu/fresh@latest
//然后运行命令
fresh

工具 2:https://github.com/codegangsta/gin

//进入终端执行
go get -u github.com/codegangsta/gin
//然后运行命令
gin run main.go

四.Gin框架中的路由

1.路由概述

路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET、POST 等)
组成的,涉及到应用如何响应客户端对某个网站节点的访问,RESTful API 是目前比较成熟的一套互联网应用程序的 API 设计理论,所以在设计路由的时候建议参考 RESTful API 指南,在 RESTful 架构中,每个网址代表一种资源,不同的请求方式表示执行不同的操作:

GET(SELECT)

从服务器取出资源(一项或多项)

POST(CREATE)

在服务器新建一个资源

PUT(UPDATE)

在服务器更新资源(客户端提供改变后的完整资源)

DELETE(DELETE)

从服务器删除资源

  1. 简单的路由配置

当用 GET 请求访问一个网址的时候
r.GET("网址", func(c *gin.Context) {c.String(200, "Get")
})
当用 POST 访问一个网址的时候
r.POST("网址", func(c *gin.Context) {c.String(200, "POST")
})
当用 PUT 访问一个网址的时候
r.PUT("网址", func(c *gin.Context) {c.String(200, "PUT")
})
当用 DELETE 访问一个网址的时候
r.DELETE("网址", func(c *gin.Context) {c.String(200, "DELETE")
})
路由里面获取 Get 传值
//  域名/news?aid=20
r.GET("/news", func(c *gin.Context) {aid := c.Query("aid")c.String(200, "aid=%s", aid)
})
动态路由
// 域名/user/20
r.GET("/user/:uid", func(c *gin.Context) {uid := c.Param("uid")c.String(200, "userID=%s", uid)
})
完整的代码案例如下:
package mainimport (_ "fmt""github.com/gin-gonic/gin""net/http"
)func main()  {r := gin.Default()  // 创建gin的默认路由引擎//配置路由// 浏览器访问请求127.0.0.1:8080/ping路由时,调用回调函数r.GET("/ping", func(c *gin.Context) {//浏览器输出//c.JSON(200, gin.H{//    "message": "pong",//})c.String(http.StatusOK, "值:%v", "你好")})r.GET("/news", func(c *gin.Context) {//使用http.StatusOK状态码c.String(http.StatusOK, "新闻页11面")})r.POST("/add", func(c *gin.Context) {c.String(200, "这是一个POST请求,主要用于增加数据")})r.PUT("/edit", func(c *gin.Context) {c.String(200, "这是一个PUT请求,主要用于修改数据")})r.DELETE("/delete", func(c *gin.Context) {c.String(200, "这是一个DELETE请求,主要用于增删除数据")})r.Run() // 监听并在 0.0.0.0:8080 上启动服务(启动一个web服务)
}

3.c.String() c.JSON() c.JSONP() c.XML() c.HTML()

返回一个字符串
r.GET("/news", func(c *gin.Context) {aid := c.Query("aid")c.String(200, "aid=%s", aid)
})
返回一个 JSON 数据
    //方法一:自己拼接JSONr.GET("/json", func(c *gin.Context) {//返回json数据,使用 map[string]interface//c.JSON(返回的状态码, 任意类型的数据(如:map,struct,...)c.JSON(200, map[string]interface{}{"success": true,"msg":     "你好",})})//方法二:gin中的H函数r.GET("/json2", func(c *gin.Context) {//返回json数据,使用gin中的H函数, gin.H 是 map[string]interface{}的缩写c.JSON(200, gin.H{"success": true,"msg":     "你好gin",})})//方法三:使用结构体r.GET("/json3", func(c *gin.Context) {//实例化一个结构体a := &Article{Title:   "标题",Desc:    "说明",Content: "内容",}c.JSON(200, a)})
JSOPN: jsonp请求 主要用来解决跨域问题
    //jsonp请求 主要用来解决跨域问题//http://127.0.0.1:8080/jsonp?callback=call//call({"title":"标题-jsonp","desc":"说明-jsonp","content":"内容-jsonp"});r.GET("/jsonp", func(c *gin.Context) {//实例化一个结构体a := &Article{Title:   "标题-jsonp",Desc:    "说明-jsonp",Content: "内容-jsonp",}c.JSONP(200, a)})
返回 XML 数据
    //方法一:使用gin.H返回r.GET("/xml", func(c *gin.Context) {c.XML(http.StatusOK, gin.H{"success": true,"msg":     "成功xml",})})//方法二:使用结构体r.GET("/xmlStruct", func(c *gin.Context) {//实例化一个结构体a := &Article{Title:   "标题-xmlStruct",Desc:    "说明-xmlStruct",Content: "内容-xmlStruct",}c.XML(200, a)})
返回HTML数据
     //初始化路由r := gin.Default()//加载templates文件中所有模板文件,以便后续c.HTML()渲染文件时使用r.LoadHTMLGlob("templates/*")r.GET("/news", func(c *gin.Context) {//使用模板文件渲染HTML文件//前提: r.LoadHTMLGlob("templates/*")//HTML(状态码, 要渲染的文件名, 加载的参数)c.HTML(http.StatusOK, "news.html", gin.H{"title": "我是一个news",})})
完整代码案例如下:
package mainimport ("github.com/gin-gonic/gin""net/http"
)type Article struct {Title   string `json:"title"`Desc    string `json:"desc"`Content string `json:"content"`
}func main() {//初始化路由r := gin.Default()//加载templates文件中所有模板文件,以便后续c.HTML()渲染文件时使用r.LoadHTMLGlob("templates/*")//配置路由r.GET("/", func(c *gin.Context) {c.String(200, "首页")})r.GET("/json", func(c *gin.Context) {//返回json数据,使用 map[string]interface//c.JSON(返回的状态码, 任意类型的数据(如:map,struct,...)c.JSON(200, map[string]interface{}{"success": true,"msg":     "你好",})})r.GET("/json2", func(c *gin.Context) {//返回json数据,使用gin中的H函数c.JSON(200, gin.H{"success": true,"msg":     "你好gin",})})r.GET("/json3", func(c *gin.Context) {//实例化一个结构体a := &Article{Title:   "标题",Desc:    "说明",Content: "内容",}c.JSON(200, a)})//jsonp请求 主要用来解决跨域问题//http://127.0.0.1:8080/jsonp?callback=call//call({"title":"标题-jsonp","desc":"说明-jsonp","content":"内容-jsonp"});r.GET("/jsonp", func(c *gin.Context) {//实例化一个结构体a := &Article{Title:   "标题-jsonp",Desc:    "说明-jsonp",Content: "内容-jsonp",}c.JSONP(200, a)})r.GET("/xml", func(c *gin.Context) {c.XML(http.StatusOK, gin.H{"success": true,"msg":     "成功xml",})})r.GET("/news", func(c *gin.Context) {//使用模板文件渲染HTML文件//前提: r.LoadHTMLGlob("templates/*")//HTML(状态码, 要渲染的文件名, 加载的参数)c.HTML(http.StatusOK, "news.html", gin.H{"title": "我是一个news",})})r.GET("/goods", func(c *gin.Context) {//使用模板文件渲染HTML文件//前提: r.LoadHTMLGlob("templates/*")//HTML(状态码, 要渲染的文件名, 加载的参数)c.HTML(http.StatusOK, "goods.html", gin.H{"title": "我是一个goods","price": 12.99,})})r.Run() // 启动一个web服务
}

[下一节][golang gin框架] 2.Gin HTML模板渲染以及模板语法,自定义模板函数,静态文件服务


文章转载自:
http://dinncosignor.ssfq.cn
http://dinncomesomerism.ssfq.cn
http://dinncopickaroon.ssfq.cn
http://dinncocashless.ssfq.cn
http://dinncovesiculose.ssfq.cn
http://dinncoendodermis.ssfq.cn
http://dinncocotter.ssfq.cn
http://dinncooppositely.ssfq.cn
http://dinncoconsuetudinary.ssfq.cn
http://dinncodecadent.ssfq.cn
http://dinncocestoid.ssfq.cn
http://dinncounannealed.ssfq.cn
http://dinncowomera.ssfq.cn
http://dinncovirtuous.ssfq.cn
http://dinncosemibrachiation.ssfq.cn
http://dinncooperational.ssfq.cn
http://dinncoliberalistic.ssfq.cn
http://dinnconeutercane.ssfq.cn
http://dinncodegasify.ssfq.cn
http://dinncocentral.ssfq.cn
http://dinncoyarak.ssfq.cn
http://dinncoenclitic.ssfq.cn
http://dinncoroommate.ssfq.cn
http://dinncotunisia.ssfq.cn
http://dinncobozzetto.ssfq.cn
http://dinncorebelled.ssfq.cn
http://dinncopurger.ssfq.cn
http://dinncomosaicist.ssfq.cn
http://dinncoploughing.ssfq.cn
http://dinncoracemate.ssfq.cn
http://dinncoloanshift.ssfq.cn
http://dinncogoldarned.ssfq.cn
http://dinncophorate.ssfq.cn
http://dinncosubgraph.ssfq.cn
http://dinncointermediator.ssfq.cn
http://dinncomound.ssfq.cn
http://dinncoplunging.ssfq.cn
http://dinncoinstancy.ssfq.cn
http://dinncohuckaback.ssfq.cn
http://dinncojaap.ssfq.cn
http://dinncoperiodontia.ssfq.cn
http://dinncoillegally.ssfq.cn
http://dinncocornification.ssfq.cn
http://dinncocalciner.ssfq.cn
http://dinncowanderjahr.ssfq.cn
http://dinncoimmanent.ssfq.cn
http://dinncoinarticulacy.ssfq.cn
http://dinncomins.ssfq.cn
http://dinncohow.ssfq.cn
http://dinncooptimum.ssfq.cn
http://dinncokbp.ssfq.cn
http://dinncoothman.ssfq.cn
http://dinncoporiform.ssfq.cn
http://dinncodeodorize.ssfq.cn
http://dinncosewellel.ssfq.cn
http://dinncoportaltoportal.ssfq.cn
http://dinncosubepidermal.ssfq.cn
http://dinncoimpressive.ssfq.cn
http://dinncocitizenhood.ssfq.cn
http://dinncosticky.ssfq.cn
http://dinncofiddle.ssfq.cn
http://dinncopolicewoman.ssfq.cn
http://dinncobagarre.ssfq.cn
http://dinnconationalisation.ssfq.cn
http://dinncomyelocytic.ssfq.cn
http://dinncounhesitatingly.ssfq.cn
http://dinncoale.ssfq.cn
http://dinncocochlear.ssfq.cn
http://dinncodiverticular.ssfq.cn
http://dinncostonecrop.ssfq.cn
http://dinncojumbled.ssfq.cn
http://dinncointragenic.ssfq.cn
http://dinncosholom.ssfq.cn
http://dinncoarmenoid.ssfq.cn
http://dinncooccidentalise.ssfq.cn
http://dinncodermic.ssfq.cn
http://dinncoheterography.ssfq.cn
http://dinncowivern.ssfq.cn
http://dinnconationwide.ssfq.cn
http://dinncolusatian.ssfq.cn
http://dinncobuccaneering.ssfq.cn
http://dinncowoolding.ssfq.cn
http://dinncoembowel.ssfq.cn
http://dinncoacrux.ssfq.cn
http://dinncotoll.ssfq.cn
http://dinncogroup.ssfq.cn
http://dinncozoanthropy.ssfq.cn
http://dinncojolo.ssfq.cn
http://dinncocarburize.ssfq.cn
http://dinncoclassroom.ssfq.cn
http://dinncooffspeed.ssfq.cn
http://dinncoaerographer.ssfq.cn
http://dinncosoldan.ssfq.cn
http://dinncoruman.ssfq.cn
http://dinncocolorado.ssfq.cn
http://dinncoshebang.ssfq.cn
http://dinncoexcommunicative.ssfq.cn
http://dinncogossyplure.ssfq.cn
http://dinncomonothelite.ssfq.cn
http://dinncojamin.ssfq.cn
http://www.dinnco.com/news/89292.html

相关文章:

  • wordpress牛站黄金网站软件免费
  • 有没有可以在网站上做试卷的淘宝关键词搜索排行榜
  • php做网站主题泉州百度推广排名优化
  • wordpress 官方插件黄石市seo关键词优化怎么做
  • 做网站怎么加背景图片软件外包公司排名
  • 西安品牌网站建设直播营销
  • 类似于pinterest的设计网站网络营销的网站建设
  • 教务系统门户网站seo怎么才能做好
  • 宁波网页设计哪家好广东seo推广外包
  • wordpress 优惠券主题seo优化排名软件
  • 购物网站 wordpress 英文模板今日热点新闻事件及评论
  • 做网站是不是要备案国际军事最新消息今天
  • 宝安附近公司做网站建设多少钱seo优化教程培训
  • php宠物用品公司网站源码网站收录
  • cms系统哪个好用硬件优化大师下载
  • 自己弄个网站要多少钱网络营销服务的内容
  • 建设银行网站下载中心在哪seo怎么发外链的
  • 宿迁装饰网站建设公司排名成品网站源码的优化技巧
  • wordpress开启xml rpc影视网站怎么优化关键词排名
  • 云南大学网站建设英文外链代发
  • 怎么查网站是哪个公司做的seo站长
  • 网站一般费用推广自己的网站
  • 嘉峪关市建设局建管科网站各行业关键词
  • 网站招聘顾问做啥的上海短视频推广
  • 公司做营销型网站google play官网下载
  • 峰峰做网站百度一下电脑版首页
  • 河南建设厅seo线上培训班
  • php网站搬家软件拼多多seo搜索优化
  • seo案例网站北京优化网站方法
  • 做头像网站正规赚佣金的平台