golang-gin框架快速入门--推荐

1.设置golangd的配置;

go env :命令后,获取安装gin的国内代理,解决访问国外网站下载包慢的问题

 1.1.检查golangd的设置

 检查上述三个地方的设置,看看是否正确,重点是工modules这个地方设置,查看是否启用了国内代理

验证是否正确安装了包以及是否包内有文件,确保程序的可用性

 1.2简单的使用:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//访问的地址,处理请求
	engine.GET("/hello", func(context *gin.Context) {
		context.JSONP(http.StatusOK, gin.H{
			"msg": "hello golang!",
		})

	})

	//服务运行的端口
	engine.Run(":9000")

}

浏览器演示结果:


2.RESTFUL API

以前写网站使用

get /user

post /create_user

post /update_user

post /delete_user

现在状态的restful api

get /user

post/user

put /user

delete /user

测试工具

postman,apifox

https://www.apifox.cn/

我用的是postman 

其余类似,具体的restful风格都是可以这样测试


3.响应前端页面内容:

3.1前端页面

 3.2后端d代码:

 3.3效果


3.4增强效果展示--css样式--背景设置

3.5增强效果展示-js-设置-背景设置

3.6index.html修改

3.7后端代码

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//加载静态页面
	engine.LoadHTMLGlob("templates/*")
	
	//加载资源文件
	engine.Static("/static","./static")
	

	//访问的地址,处理请求,RESTFUL
	engine.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "这个是go后台传给前端的数据",
		})

	})

	//服务运行的端口
	engine.Run(":9000")

}

3.8展示


5.获取前端请求参数

5.1第一种方式:“”“

//http://127.0.0.1:9000/user/info?userid=100&name=tom
	//接收前端传过来的参数的方式
	//http://127.0.0.1:9000/user/info?userid=100&name=tom
	engine.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		name := context.Query("name")
		context.JSON(http.StatusOK, gin.H{
			"userid": userid,
			"name":   name,
		})

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//加载静态页面
	engine.LoadHTMLGlob("templates/*")

	//加载资源文件
	engine.Static("/static", "./static")

	//访问的地址,处理请求,RESTFUL
	engine.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "这个是go后台传给前端的数据",
		})

	})

	//接收前端传过来的参数的方式
	//http://127.0.0.1:9000/user/info?userid=100&name=tom
	engine.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		name := context.Query("name")
		context.JSON(http.StatusOK, gin.H{
			"userid": userid,
			"name":   name,
		})

	})

	//服务运行的端口
	engine.Run(":9000")

}

 5.2第二种前端传过来的参数形式

//第二种方式:接收前端传过来的参数的方式
	//  /user/info/1/tom

5.2   “127.0.0.1:9000/user/info/100/tom

	//接收前端传过来的参数的方式
	//  /user/info/1/tom
	engine.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,
		})

	})

全部代码:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//加载静态页面
	engine.LoadHTMLGlob("templates/*")

	//加载资源文件
	engine.Static("/static", "./static")

	//访问的地址,处理请求,RESTFUL
	engine.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "这个是go后台传给前端的数据",
		})

	})

	//接收前端传过来的参数的方式
	//http://127.0.0.1:9000/user/info?userid=100&name=tom
	engine.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		name := context.Query("name")
		context.JSON(http.StatusOK, gin.H{
			"userid": userid,
			"name":   name,
		})

	})

	//接收前端传过来的参数的方式
	//  /user/info/1/tom
	engine.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,
		})

	})

	//服务运行的端口
	engine.Run(":9000")

}

展示:

5.3第三种方式:前端给后端传递json数据

package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//加载静态页面
	engine.LoadHTMLGlob("templates/*")

	//加载资源文件
	engine.Static("/static", "./static")

	//访问的地址,处理请求,RESTFUL
	engine.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "这个是go后台传给前端的数据",
		})

	})

	//1.第一种方式:接收前端传过来的参数的方式
	//http://127.0.0.1:9000/user/info?userid=100&name=tom
	engine.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		name := context.Query("name")
		context.JSON(http.StatusOK, gin.H{
			"userid": userid,
			"name":   name,
		})

	})

	//第二种方式:接收前端传过来的参数的方式
	//  /user/info/1/tom
	engine.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

	engine.POST("/json", func(context *gin.Context) {
		//request.body
		data, _ := context.GetRawData()

		var m map[string]interface{}
		err := json.Unmarshal(data, &m)
		if err != nil {
			log.Fatalln("info err...")
		}
		context.JSON(http.StatusOK, m)

	})

	//服务运行的端口
	engine.Run(":9000")

}

展示:前端传送json文件到后端;

 5.4第四种方式,获取前端的表单传送的数据,到后端

前端表单数据

<!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>golang 语言的学习</h1>

<form action="/user/add" method="post">

    <p>username:<input type="text" name="username"></p>
    <p>password:<input type="password" name="password"></p>
   <button type="submit">提交</button>
</form>

</body>
</html>

 后端代码

后端全部代码

package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//加载静态页面
	engine.LoadHTMLGlob("templates/*")

	//加载资源文件
	engine.Static("/static", "./static")

	//访问的地址,处理请求,RESTFUL
	engine.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "这个是go后台传给前端的数据",
		})

	})

	//1.第一种方式:接收前端传过来的参数的方式
	//http://127.0.0.1:9000/user/info?userid=100&name=tom
	engine.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		name := context.Query("name")
		context.JSON(http.StatusOK, gin.H{
			"userid": userid,
			"name":   name,
		})

	})

	//第二种方式:接收前端传过来的参数的方式
	//  /user/info/1/tom
	engine.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

	engine.POST("/json", func(context *gin.Context) {
		//request.body
		data, _ := context.GetRawData()

		var m map[string]interface{}
		err := json.Unmarshal(data, &m)
		if err != nil {
			log.Fatalln("info err...")
		}
		context.JSON(http.StatusOK, m)

	})

	//第四种传输方式,前端传送表单数据
	engine.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,
		})
	})

	//服务运行的端口
	engine.Run(":9000")

}


6.路由方式:

6.1  301重定向

6.2任何没有的路由的   404页面

package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//加载静态页面
	engine.LoadHTMLGlob("templates/*")

	//加载资源文件
	engine.Static("/static", "./static")

	//访问的地址,处理请求,RESTFUL
	engine.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "这个是go后台传给前端的数据",
		})

	})

	//1.第一种方式:接收前端传过来的参数的方式
	//http://127.0.0.1:9000/user/info?userid=100&name=tom
	engine.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		name := context.Query("name")
		context.JSON(http.StatusOK, gin.H{
			"userid": userid,
			"name":   name,
		})

	})

	//第二种方式:接收前端传过来的参数的方式
	//  /user/info/1/tom
	engine.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

	engine.POST("/json", func(context *gin.Context) {
		//request.body
		data, _ := context.GetRawData()

		var m map[string]interface{}
		err := json.Unmarshal(data, &m)
		if err != nil {
			log.Fatalln("info err...")
		}
		context.JSON(http.StatusOK, m)

	})

	//第四种传输方式,前端传送表单数据
	engine.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,
		})
	})

	//路由方式,重定向301
	engine.GET("/route1", func(context *gin.Context) {
		context.Redirect(http.StatusMovedPermanently, "https://blog.csdn.net/wtt234/category_10962787.html")

	})

	//404 error

	engine.NoRoute(func(context *gin.Context) {
		context.HTML(http.StatusNotFound, "404.html", nil)
	})

	//服务运行的端口
	engine.Run(":9000")

}

6.3路由组

代码--user组里面的add,update

展示

 6.3.2代码组

 order组:

package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	//创建服务
	engine := gin.Default()

	//加载静态页面
	engine.LoadHTMLGlob("templates/*")

	//加载资源文件
	engine.Static("/static", "./static")

	//访问的地址,处理请求,RESTFUL
	engine.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "这个是go后台传给前端的数据",
		})

	})

	//1.第一种方式:接收前端传过来的参数的方式
	//http://127.0.0.1:9000/user/info?userid=100&name=tom
	engine.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		name := context.Query("name")
		context.JSON(http.StatusOK, gin.H{
			"userid": userid,
			"name":   name,
		})

	})

	//第二种方式:接收前端传过来的参数的方式
	//  /user/info/1/tom
	engine.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

	engine.POST("/json", func(context *gin.Context) {
		//request.body
		data, _ := context.GetRawData()

		var m map[string]interface{}
		err := json.Unmarshal(data, &m)
		if err != nil {
			log.Fatalln("info err...")
		}
		context.JSON(http.StatusOK, m)

	})

	//第四种传输方式,前端传送表单数据
	engine.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,
		})
	})

	//路由方式,重定向301
	engine.GET("/route1", func(context *gin.Context) {
		context.Redirect(http.StatusMovedPermanently, "https://blog.csdn.net/wtt234/category_10962787.html")

	})

	//404 error

	engine.NoRoute(func(context *gin.Context) {
		context.HTML(http.StatusNotFound, "404.html", nil)
	})

	//路由组   /user/add  用户组
	usergroup := engine.Group("/user")
	{
		usergroup.GET("/add", func(context *gin.Context) {
			context.JSON(http.StatusOK, "user-add")
		})
		usergroup.GET("/update", func(context *gin.Context) {
			context.JSON(http.StatusOK, "user-update")
		})
	}
	//order订单组
	ordergroup := engine.Group("/order")
	{
		ordergroup.GET("/add", func(context *gin.Context) {
			context.JSON(http.StatusOK, "user-add")
		})
		ordergroup.GET("/update", func(context *gin.Context) {
			context.JSON(http.StatusOK, "user-update")
		})
	}

	//服务运行的端口
	engine.Run(":9000")

}

7.中间件(拦截器),每个语言里面称呼不一样,但是实质一样

7.1自定义自己的拦截器

 中间件的使用

 

 前端调取以及返回结果:

 后台获取的数据

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值