当前位置:首页 > CN2资讯 > 正文内容

windows 10 go语言下载国内镜像

12小时前CN2资讯


Go语言GIN框架安装与入门


文章目录

  • Go语言GIN框架安装与入门
  • 1. 创建配置环境
  • 2. 配置环境
  • 3. 下载最新版本Gin
  • 4. 编写第一个接口
  • 5. 静态页面和资源文件加载
  • 6. 各种传参方式
  • 6.1 URL传参
  • 6.2 路由形式传参
  • 6.3 前端给后端传递JSON格式
  • 6.4 表单形式传参
  • 7. 路由和路由组
  • 8. 项目代码main.go
  • 9. 总结



之前学习了一周的GO语言,学会了GO语言基础,现在尝试使用GO语言最火的框架GIN来写一些简单的接口。看了B站狂神说的GIN入门视频,基本明白如何写接口了,下面记录一下基本的步骤。

1. 创建配置环境

我们使用Goland创建第一个新的开发环境,这里只要在windows下面安装好Go语言,Goroot都能自动识别。


新的项目也就只有1个go.mod的文件,用来表明项目中使用到的第三方库。

2. 配置环境

我们使用第三方库是需要从github下载的,但是github会经常连不上,所以我们就需要先配置第三方的代理地址。我们再Settings->Go->Go Modules->Environment下面配上代理地址。

GOPROXY=https://goproxy.cn,direct

3. 下载最新版本Gin

在IDE里面的Terminal下面安装Gin框架,使用下面的命令安装Gin,安装完成以后,go.mod下面require就会自动添加依赖。

go get -u /gin-gonic/gin

4. 编写第一个接口

创建main.go文件,然后编写以下代码,这里定义了一个/hello的路由。

package main import "/gin-gonic/gin" func 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 lang="en"> <head> <meta charset="UTF-8"> <title>我的第一个GO web页面</title> <link rel="stylesheet" href="/static/css/style.css"> <script src="/static/js/common.js"></script> </head> <body> <h1>谢谢大家支持</h1> 获取后端的数据为: {{.msg}} <form action="/user/add" method="post"> <p>username: <input type="text" name="username"></p> <p>password: <input type="text" name="password"></p> <button type="submit"> 提 交 </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?userid=123&username=dfa ginSever.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/dfa ginSever.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格式

// 前端给后端传递json ginSever.POST("/json", func(context *gin.Context) { // request.body data, _ := 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") }) // 404 ginSever.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 main import ( "encoding/json" "/gin-gonic/gin" "log" "net/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?userid=123&username=dfa ginSever.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/dfa ginSever.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, }) }) // 前端给后端传递json ginSever.POST("/json", func(context *gin.Context) { // request.body data, _ := 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") }) // 404 ginSever.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") }



    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/28278.html

    分享给朋友:

    “windows 10 go语言下载国内镜像” 的相关文章

    日本VPS全面解析:高性能、低延迟的最佳选择

    日本VPS因其独特的地理位置和卓越的性能,成为许多用户的首选。日本作为亚洲的科技中心,拥有先进的网络基础设施和稳定的电力供应,这为VPS服务提供了坚实的基础。无论是个人用户还是企业用户,日本VPS都能满足多样化的需求。 日本VPS的优势 日本VPS的最大优势在于其地理位置。日本位于亚洲的中心地带,连...

    VPSDime评测:高性价比的VPS服务选择

    VPSDime概述 在如今互联网发展的浪潮中,各种主机服务商层出不穷,VPSDime作为一家成立于2013年的海内外主机服务商,引起了我的关注。它隶属于Nodisto IT,专注于VPS业务,提供多种类型的虚拟专用服务器。这对我这样的用户来说,选择合适的主机服务显得尤为重要,尤其是对于需要高性能和高...

    如何在VPS上启用和配置IPv6以提升网络性能

    在当今数字化的时代,互联网已经成为我们日常生活中不可或缺的一部分。随着设备和用户数量的快速增长,现有的IPv4地址开始捉襟见肘。这时,IPv6(Internet Protocol Version 6)应运而生,作为下一代互联网协议,它的出现可以说是一种必然趋势。IPv6不仅解决了IPv4地址耗尽的问...

    深入了解ICMP协议及其在网络管理中的应用

    ICMP(Internet Control Message Protocol,互联网控制消息协议)是TCP/IP协议族中的一种重要网络协议。我们可以把ICMP想象成网络中的信使,它主要负责在网络中传递控制消息和错误报告。这种功能对于维护网络的正常运作至关重要,让网络管理员能够及时发现并处理问题。IC...

    全面解析VPS测评:如何选择最佳虚拟专用服务器

    了解VPS(虚拟专用服务器)对许多人来说并不陌生。在我们的网络环境中,VPS作为一种重要的服务器解决方案,广泛应用于网站托管、应用开发、以及各种在线服务的支持。VPS让用户可以在共享环境中获得类似独立服务器的资源,提供了灵活性和更好的性能。与共享主机相比,VPS的显著优势在于更高的资源保障和自定义能...

    WordPress reCAPTCHA插件:提升网站安全与用户体验的最佳解决方案

    reCAPTCHA插件概述 在今天的网络环境中,安全性愈发重要,尤其是对于使用WordPress的网站。WordPress reCAPTCHA插件成为了一种流行的解决方案,它借助Google强大的reCAPTCHA服务,帮助我们有效地区分真实用户和可能扰乱网站的机器程序。在我接触这个插件之后,发现它...