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

news/2024/4/27 23:20:42/文章来源:https://blog.csdn.net/wtt234/article/details/127518069

1.设置golangd的配置;

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

 1.1.检查golangd的设置

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

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

 1.2简单的使用:

package mainimport ("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 mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {//创建服务engine := gin.Default()//加载静态页面engine.LoadHTMLGlob("templates/*")//加载资源文件engine.Static("/static","./static")//访问的地址,处理请求,RESTFULengine.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=tomengine.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 mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {//创建服务engine := gin.Default()//加载静态页面engine.LoadHTMLGlob("templates/*")//加载资源文件engine.Static("/static", "./static")//访问的地址,处理请求,RESTFULengine.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=tomengine.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/tomengine.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 mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {//创建服务engine := gin.Default()//加载静态页面engine.LoadHTMLGlob("templates/*")//加载资源文件engine.Static("/static", "./static")//访问的地址,处理请求,RESTFULengine.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=tomengine.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/tomengine.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 mainimport ("encoding/json""github.com/gin-gonic/gin""log""net/http"
)func main() {//创建服务engine := gin.Default()//加载静态页面engine.LoadHTMLGlob("templates/*")//加载资源文件engine.Static("/static", "./static")//访问的地址,处理请求,RESTFULengine.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=tomengine.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/tomengine.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,})})//第三种方式:前端给后端传递jsonengine.POST("/json", func(context *gin.Context) {//request.bodydata, _ := 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 mainimport ("encoding/json""github.com/gin-gonic/gin""log""net/http"
)func main() {//创建服务engine := gin.Default()//加载静态页面engine.LoadHTMLGlob("templates/*")//加载资源文件engine.Static("/static", "./static")//访问的地址,处理请求,RESTFULengine.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=tomengine.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/tomengine.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,})})//第三种方式:前端给后端传递jsonengine.POST("/json", func(context *gin.Context) {//request.bodydata, _ := 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 mainimport ("encoding/json""github.com/gin-gonic/gin""log""net/http"
)func main() {//创建服务engine := gin.Default()//加载静态页面engine.LoadHTMLGlob("templates/*")//加载资源文件engine.Static("/static", "./static")//访问的地址,处理请求,RESTFULengine.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=tomengine.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/tomengine.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,})})//第三种方式:前端给后端传递jsonengine.POST("/json", func(context *gin.Context) {//request.bodydata, _ := 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,})})//路由方式,重定向301engine.GET("/route1", func(context *gin.Context) {context.Redirect(http.StatusMovedPermanently, "https://blog.csdn.net/wtt234/category_10962787.html")})//404 errorengine.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 mainimport ("encoding/json""github.com/gin-gonic/gin""log""net/http"
)func main() {//创建服务engine := gin.Default()//加载静态页面engine.LoadHTMLGlob("templates/*")//加载资源文件engine.Static("/static", "./static")//访问的地址,处理请求,RESTFULengine.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=tomengine.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/tomengine.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,})})//第三种方式:前端给后端传递jsonengine.POST("/json", func(context *gin.Context) {//request.bodydata, _ := 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,})})//路由方式,重定向301engine.GET("/route1", func(context *gin.Context) {context.Redirect(http.StatusMovedPermanently, "https://blog.csdn.net/wtt234/category_10962787.html")})//404 errorengine.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自定义自己的拦截器

 中间件的使用

 

 前端调取以及返回结果:

 后台获取的数据

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_218424.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

git实用操作:git rebase -i 合并多个 commit

我们开发的过程中&#xff0c;可能会有多次的修补提交&#xff0c;就会出现多条提交记录和备注信息&#xff0c;此时我们可以使用 git rebase -i来合并多个commit&#xff0c;以简化提交记录 1.合并最近的 4 次提交纪录&#xff0c;执行&#xff1a; git rebase -i HEAD~42.自…

今天不想上班

无语 - - 为什么项目迁移了 算了 不需要脑子思考复杂的 反正我干就是了&#xff0c;领了工资干了活&#xff0c;我菜那是我的事。 顺便学习一下好了。 可能我基础不太行吧。 关于我和同事新一起合作写一个项目 老板让我优化代码后老板满意了同事被封装的痛苦了。 现在…

TortoiseSVN下载安装及问题总结

文章目录TortoiseSVN介绍下载安装问题使用首先将客户端与服务器进行连接检出提交TortoiseSVN介绍 TortoiseSVN是一个开源的版本控制系统&#xff0c;也就是说Subversion管理者随着时间而改变的数据。这些数据放置在任何一个中央资料档案库&#xff08;repository&#xff09;中…

中国定制家具行业深度调研及投资前景预测报告

欧派家居VS索菲亚:家具商业的布局史 家具产品的特性决定了家具产业在经济和社会中的重要地位。随着经济的不断发展&#xff0c;家具行业也发展迅速。目前&#xff0c;欧派家居和索菲亚是国内家具行业的龙头企业主。 2.家具企业布局及经营状况: ——家具经营类型:索菲亚、欧派…

Apollo星火计划学习笔记第四讲1——Apollo高精地图模块

Apollo学习笔记零、目录一、高精地图的作用1.1 相对于传统地图二、高精地图介绍2.1 高精地图构成2.2 高精地图制作2.2.1 地图采集2.2.2 地图制作2.2.3 地图标注工具2.2.4 地图保存三、Apollo高精度地图3.1 高精地图目录结构3.2 高精地图格式3.3 Apollo高精地图API接口四、实践案…

【Gateway】统一网关Gateway学习记录

目录 网关能干什么 网关的技术实现 搭建网关服务 网关作用流程图 路由断言工厂&#xff08;Route Predicate Factory&#xff09; gateway中有三种过滤器&#xff1a; 1. 默认过滤器&#xff08;DefaultFiter&#xff09; 2. 路由过滤器&#xff08;GatewayFilter&…

3. Longest Substring Without Repeating Characters (无重复字符的最长子串)滑动窗口

文章目录问题英文中文代码小白的码大佬的码知识点unordered_set 容器具有以下几个特性&#xff1a;总结问题 英文 3. Longest Substring Without Repeating Characters (无重复字符的最长子串) 中文 代码 小白的码 #include <iostream> #include <string> #…

Terraform 基础 申请阿里云资源

之前&#xff0c;资源都定义好了&#xff0c;现在就是去申请资源了。 申请这些资源就需要使用terraform的命令行了&#xff0c;开始初始化后端&#xff0c;后端是有存储文件的&#xff0c;默认情况下是在本地存储的&#xff0c;然后会多一些文件。 &#xff08;下载插件&#x…

在python中安装gensim包(为了使用LDA)

LDA是英文“Latent Dirichlet Allocation”的缩写&#xff0c;意思是隐含狄利克雷分布&#xff0c;是一种主题模型&#xff08;topic model&#xff09;&#xff0c;它可以将文档集中每篇文档的主题以概率分布的形式给出。 gensim包中有LDA的一种实现。 本文介绍gensim包的安…

神经网络中的算法-梯度下降算法

目录 一、概述 二、算法思想 1、一维 2、多维 三、梯度下降类型 1、批量梯度下降算法 2、随机梯度下降算法 3、小批量梯度下降算法 一、概述 梯度下降法&#xff08;Gradient descent &#xff09;是一个一阶最优化算法&#xff0c;通常也称为最陡下降法 &am…

NetworkManager nmcli ipv4 静态ip 笔记221025

nmcli connection modify 可以修改现有连接 con 可以写成 c 到 connection 之间的字段mod 可以写成 m 到 modify 之间的字段nmcli connection modify nmcli connec modify nmcli conne modif nmcii conn modi nmcli con mod nmcli co mo nmcli c m nmcli c modify nmcli conne…

购物中心智能管理系统该如何选择

快鲸智慧楼宇系统作为新一代数智化商管系统&#xff0c;以实际业务场景出发构建产品逻辑&#xff0c;并在传统商管系统基础上&#xff0c;拥有独家的商业大数据加持&#xff0c;同时嵌入了BI智能分析工具&#xff0c;打造了一个招商营运场景的数智化系统&#xff0c;将“人的经…

[C++] 初接触 泛型编程—— C++ 模板分析

泛型编程 C中引入了重载的概念&#xff0c;使得可以编写多个函数名相同但参数、返回值不同的函数&#xff0c;例如&#xff1a; 相同的函数名可以传入不同的参宿&#xff0c;进而调用不同的函数 但&#xff0c;即使有了重载&#xff0c;相同功能的函数 还要分别对不同的类型进…

Python之numpy数组篇(下)

目录 一、数组排序 1、概念 2、升序&#xff0c;最大、最小值 3、原地、横向排序 二、数组内积运算 1、概念 2、代码例子 三、访问数组元素 1、使用介绍 2、行列直接访问 3、切片 4、行列访问扩展 四、数组对函数运算的支持 1、概念 2、例子 五、改变数组形状 1…

1.3.3系统调用

文章目录为什么引入系统调用什么是系统调用系统调用和库函数的区别系统调用的背后为什么引入系统调用 为了防止这样情况的发生&#xff0c;就是防止进程能够随意的去调用我们的系统资源&#xff0c;操作系统提供了系统调用的功能&#xff0c;用户进程想要使用打印机这种共享资源…

12_Vue事件总结

事件总结 事件修饰符连携 准备工作 html <!-- 定义一个容器 --><div class="app"><!-- 事件修饰符连携 --><div class="box" @click="toBaidu"><a href="https://www.baidu.com" @click.stop="toBaid…

Java代码审计前置知识——SpringMVC基础

目录 (一&#xff09;回顾MVC 1.1 什么是MVC Model&#xff08;模型&#xff09; View&#xff08;视图&#xff09; Controller&#xff08;控制器&#xff09; 1.2 Model1时代 1.3 Model2时代 总结 1.4 回顾Servlet 0x01 新建一个Maven工程当做父工程,pom依赖 0x0…

1.1.2操作系统的特征

操作系统是一个系统软件&#xff0c;但与其他系统软件和应用软件有很大的不同&#xff0c;就是它拥有自己的特殊性&#xff0c;及基本特征 首先共享和并发是相互存在的条件共享和并发是虚拟和异步的前提&#xff0c;是操作系统的两个最基本的特征 1并发 拿餐厅吃饭举例子&…

3.3.3JavaScript网页编程——WebAPI(JS之BOM含正则)

目录BOMwindow对象定时器-延时函数setTimeoutJS执行机制&#xff08;执行栈、任务队列&#xff09;面试要问location对象location.href (获取完整url或者赋值)location.search (获取?后面的)location.hash(获取#号后面的)location.reloadnavigator对象&#xff08;检测浏览器移…

10_事件处理阶段

v-on指令 语法 v-on:xxx 这里的xxx指代的是各类事件类型,例如单击,双击,鼠标悬停,键盘监听等等...... 准备工作 准备一个容器,两个按钮,一个按钮不传递参数,另一个按钮传递参数 <body><!-- 创建一个容器 --><div class="subject"><!-- 标…