邯郸景区网站制作,wordpress空俩格,cms,贵州省住房和城乡建设厅官方网站介绍
Gin 是一个用 Go (Golang) 编写的 Web 框架。 它具有类似 martini 的 API#xff0c;性能要好得多#xff0c;多亏了 httprouter#xff0c;速度提高了 40 倍。 如果您需要性能和良好的生产力#xff0c;您一定会喜欢 Gin。
安装
go get -u github.com/gin-gonic/g…介绍
Gin 是一个用 Go (Golang) 编写的 Web 框架。 它具有类似 martini 的 API性能要好得多多亏了 httprouter速度提高了 40 倍。 如果您需要性能和良好的生产力您一定会喜欢 Gin。
安装
go get -u github.com/gin-gonic/gin
快速开始
package mainimport (github.com/gin-gonic/gin
)func main() {r : gin.Default()r.GET(/ping, func(c *gin.Context) {c.JSON(200, gin.H{message: pong,})})r.Run() // 监听并在 0.0.0.0:8080 上启动服务
} 接受请求参数
公共部分
package mainimport (github.com/gin-gonic/ginnet/http
)func main() {router : gin.Default()//调用方法router.Run(:8080)
}
get请求
获取路径上数据post一样
使用gin.Context.Query(参数名)获取
func main() {router : gin.Default()testGetUrl(router)router.Run(:8080)
}
func testGetUrl(router *gin.Engine) {router.GET(/getUrl, func(c *gin.Context) {// 获取参数值name : c.Query(name)age : c.Query(age)// 返回响应c.JSON(http.StatusOK, gin.H{name: name,age: age,})})
} 获取路由参数post一样
package mainimport (github.com/gin-gonic/ginionet/httpnet/urlos
)func main() {router : gin.Default()testRouteParams(router)router.Run(:8080)
}
func testRouteParams(router *gin.Engine) {router.GET(/routeParams/:name/:age, func(c *gin.Context) {// 获取参数值name : c.Param(name)age : c.Param(age)// 返回响应c.JSON(http.StatusOK, gin.H{name: name,age: age,})})
} 获取cookiepost一样
package mainimport (github.com/gin-gonic/ginionet/httpnet/urlos
)func main() {router : gin.Default()testCookies(router)router.Run(:8080)
}
func testCookies(router *gin.Engine) {router.GET(/cookies, func(c *gin.Context) {// 获取参数值name, _ : c.Cookie(name)age, _ : c.Cookie(age)// 返回响应c.JSON(http.StatusOK, gin.H{name: name,age: age,})})
} 获取表单参数post一样
func main() {router : gin.Default()testGetForm(router)router.Run(:8080)
}
func testGetForm(router *gin.Engine) {router.GET(/getForm, func(c *gin.Context) {var form User// 在这种情况下将自动选择合适的绑定if c.ShouldBind(form) nil {// 返回响应c.JSON(http.StatusOK, gin.H{name: form.Name,age: form.Age,})}})
} 获取请求头post一样
func main() {router : gin.Default()testGetHeader(router)router.Run(:8080)
}
func testGetHeader(router *gin.Engine) {router.GET(/getHeader, func(c *gin.Context) {// 获取参数值name : c.GetHeader(name)age : c.GetHeader(age)contentType : c.GetHeader(Content-Type)// 返回响应c.JSON(http.StatusOK, gin.H{name: name,age: age,contentType: contentType,})})
} post请求 获取请求体数据
和post表单一样
func main() {router : gin.Default()testPostForm(router)router.Run(:8080)
}
func testPostForm(router *gin.Engine) {router.POST(/postForm, func(c *gin.Context) {var form User// 在这种情况下将自动选择合适的绑定if c.ShouldBind(form) nil {// 返回响应c.JSON(http.StatusOK, gin.H{name: form.Name,age: form.Age,})}})
} 文件上传下载
上传单个文件
package mainimport (github.com/gin-gonic/ginnet/http
)func main() {router : gin.Default()testUpload(router)router.Run(:8080)
}
func testUpload(router *gin.Engine) {// 为 multipart forms 设置较低的内存限制 (默认是 32 MiB)router.MaxMultipartMemory 8 20 // 8 MiBrouter.POST(/upload, func(c *gin.Context) {// 单文件file, _ : c.FormFile(file)dst : file.Filename// 上传文件至指定的完整文件路径c.SaveUploadedFile(file, dst)// 返回响应c.JSON(http.StatusOK, gin.H{name: file.Filename,})})
} 上传多个文件
package mainimport (github.com/gin-gonic/ginnet/http
)func main() {router : gin.Default()testUploadMany(router)router.Run(:8080)
}
func testUploadMany(router *gin.Engine) {// 为 multipart forms 设置较低的内存限制 (默认是 32 MiB)router.MaxMultipartMemory 8 20 // 8 MiBrouter.POST(/uploadMany, func(c *gin.Context) {// Multipart formform, _ : c.MultipartForm()fileNames : make([]string, 0)files : form.File[fileList]for _, file : range files {// 上传文件至指定目录c.SaveUploadedFile(file, filedir/file.Filename)fileNames append(fileNames, file.Filename)}// 返回响应c.JSON(http.StatusOK, gin.H{name: fileNames,})})
} 下载文件
package mainimport (github.com/gin-gonic/ginionet/httpnet/urlos
)func main() {router : gin.Default()testDownload(router)router.Run(:8080)
}
func testDownload(router *gin.Engine) {router.POST(/download, func(c *gin.Context) {// 获取文件路径targetFileName : 照片1.pngfilePath : filedir/ targetFileName// 检查文件是否存在_, err : os.Stat(filePath)if os.IsNotExist(err) {c.String(http.StatusNotFound, File not found)return}//中文名称会乱码导致下载失败所以需要编码name : url.QueryEscape(targetFileName)// 设置响应头指定文件名和内容类型c.Header(Content-Disposition, attachment; filenamename)c.Header(Content-Type, application/octet-stream)// 打开文件file, err : os.Open(filePath)if err ! nil {c.String(http.StatusInternalServerError, Error opening file)return}defer file.Close()// 将文件内容写入响应体_, err io.Copy(c.Writer, file)if err ! nil {c.String(http.StatusInternalServerError, Error copying file)return}})
} 由于是postman下载的没法解析为中文用浏览器下载就没问题了
日志
func main() {// 禁用控制台颜色将日志写入文件时不需要控制台颜色。gin.DisableConsoleColor()// 记录到文件。f, _ : os.Create(gin.log)gin.DefaultWriter io.MultiWriter(f)// 如果需要同时将日志写入文件和控制台请使用以下代码。// gin.DefaultWriter io.MultiWriter(f, os.Stdout)router : gin.Default()router.GET(/ping, func(c *gin.Context) {c.String(200, pong)})router.Run(:8080)
}
路由组
func main() {router : gin.Default()// 简单的路由组: v1v1 : router.Group(/v1){v1.POST(/login, loginEndpoint)v1.POST(/submit, submitEndpoint)v1.POST(/read, readEndpoint)}// 简单的路由组: v2v2 : router.Group(/v2){v2.POST(/login, loginEndpoint)v2.POST(/submit, submitEndpoint)v2.POST(/read, readEndpoint)}router.Run(:8080)
}
运行多个服务
package mainimport (lognet/httptimegithub.com/gin-gonic/gingolang.org/x/sync/errgroup
)var (g errgroup.Group
)func router01() http.Handler {e : gin.New()e.Use(gin.Recovery())e.GET(/, func(c *gin.Context) {c.JSON(http.StatusOK,gin.H{code: http.StatusOK,error: Welcome server 01,},)})return e
}func router02() http.Handler {e : gin.New()e.Use(gin.Recovery())e.GET(/, func(c *gin.Context) {c.JSON(http.StatusOK,gin.H{code: http.StatusOK,error: Welcome server 02,},)})return e
}func main() {server01 : http.Server{Addr: :8080,Handler: router01(),ReadTimeout: 5 * time.Second,WriteTimeout: 10 * time.Second,}server02 : http.Server{Addr: :8081,Handler: router02(),ReadTimeout: 5 * time.Second,WriteTimeout: 10 * time.Second,}g.Go(func() error {return server01.ListenAndServe()})g.Go(func() error {return server02.ListenAndServe()})if err : g.Wait(); err ! nil {log.Fatal(err)}
}
重定向
HTTP 重定向很容易。 内部、外部重定向均支持。
r.GET(/test, func(c *gin.Context) {c.Redirect(http.StatusMovedPermanently, http://www.google.com/)
})通过 POST 方法进行 HTTP 重定向。请参考 issue#444
r.POST(/test, func(c *gin.Context) {c.Redirect(http.StatusFound, /foo)
})路由重定向使用 HandleContext
r.GET(/test, func(c *gin.Context) {c.Request.URL.Path /test2r.HandleContext(c)
})
r.GET(/test2, func(c *gin.Context) {c.JSON(200, gin.H{hello: world})
}) 文章转载自: http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.plnry.cn.gov.cn.plnry.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.yrycb.cn.gov.cn.yrycb.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.nmymn.cn.gov.cn.nmymn.cn http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.bnpn.cn.gov.cn.bnpn.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.smcfk.cn.gov.cn.smcfk.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.khpx.cn.gov.cn.khpx.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.hxftm.cn.gov.cn.hxftm.cn