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

1.设置golangd的配置;

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

 1.1.检查golangd的设置

golang-gin框架快速入门--推荐_第1张图片

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

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

golang-gin框架快速入门--推荐_第2张图片

 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")

}

浏览器演示结果:

golang-gin框架快速入门--推荐_第3张图片


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 

golang-gin框架快速入门--推荐_第4张图片

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

golang-gin框架快速入门--推荐_第5张图片


3.响应前端页面内容:

3.1前端页面

golang-gin框架快速入门--推荐_第6张图片

 3.2后端d代码:

golang-gin框架快速入门--推荐_第7张图片

 3.3效果

golang-gin框架快速入门--推荐_第8张图片


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

golang-gin框架快速入门--推荐_第9张图片

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

golang-gin框架快速入门--推荐_第10张图片

3.6index.html修改

golang-gin框架快速入门--推荐_第11张图片

3.7后端代码

golang-gin框架快速入门--推荐_第12张图片

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展示

golang-gin框架快速入门--推荐_第13张图片


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,
		})

golang-gin框架快速入门--推荐_第14张图片

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")

}

golang-gin框架快速入门--推荐_第15张图片

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

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

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

golang-gin框架快速入门--推荐_第16张图片

	//接收前端传过来的参数的方式
	//  /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")

}

展示:

golang-gin框架快速入门--推荐_第17张图片

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

golang-gin框架快速入门--推荐_第18张图片

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文件到后端;

golang-gin框架快速入门--推荐_第19张图片

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

前端表单数据




    
    go web

    
    



golang 语言的学习

username:

password:

 后端代码

golang-gin框架快速入门--推荐_第20张图片

后端全部代码

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")

}

golang-gin框架快速入门--推荐_第21张图片

golang-gin框架快速入门--推荐_第22张图片


6.路由方式:

6.1  301重定向

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

golang-gin框架快速入门--推荐_第23张图片

golang-gin框架快速入门--推荐_第24张图片

golang-gin框架快速入门--推荐_第25张图片

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

golang-gin框架快速入门--推荐_第26张图片

展示

golang-gin框架快速入门--推荐_第27张图片

 6.3.2代码组

 order组:

golang-gin框架快速入门--推荐_第28张图片

golang-gin框架快速入门--推荐_第29张图片

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自定义自己的拦截器

golang-gin框架快速入门--推荐_第30张图片

 中间件的使用

golang-gin框架快速入门--推荐_第31张图片 

 前端调取以及返回结果:

golang-gin框架快速入门--推荐_第32张图片

 后台获取的数据

golang-gin框架快速入门--推荐_第33张图片

 

 

你可能感兴趣的:(go语言web框架,Job,golang,gin,开发语言)