当前位置:网站首页>gin -get请求的小示例1-Handle处理GET请求
gin -get请求的小示例1-Handle处理GET请求
2022-04-23 01:13:00 【夸父追梦@】
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
//在gin框架中,Engine被定义成为一个结构体,Engine代表gin框架的一个结构体定义,
//其中包含了路由组、中间件、页面渲染接口、框架配置设置等相关内容。默认的Engine可以通过gin.Default进行创建,
//或者使用gin.New()同样可以创建。两种方式如下所示:
//engine1 = gin.Default()
//engine2 = gin.New()
//gin.Default()和gin.New()的区别在于gin.Default也使用gin.New()创建engine实例,但是会默认使用Logger和Recovery中间件。
//
//Logger是负责进行打印并输出日志的中间件,方便开发者进行程序调试;Recovery中间件的作用是如果程序执行过程中遇到panic中断了服务,
//则Recovery会恢复程序执行,并返回服务器500内部错误。通常情况下,我们使用默认的gin.Default创建Engine实例。
//HTTP请求类型
//http协议中一共定义了八种方法或者称之为类型来表明对请求网络资源(Request-URI)的不同的操作方式,分别是:
//OPTIONS、HEAD、GET、POST、PUT、DELETE、TRACE、CONNECT。
func main() {
engine := gin.Default()
engine.Handle("GET", "/hello", func(context *gin.Context) {
//获取请求接口[]
fmt.Println(context.FullPath())
//获取字符串参数 浏览器调用格式://http://127.0.0.1:8889/hello?name=tom
name := context.DefaultQuery("name", "") //有name就是返回name,没有的话返回默认值“”
fmt.Println(name)
//输出到前端
context.Writer.Write([]byte("Hello ," + name))
})
engine.Run(":8889")
}

版权声明
本文为[夸父追梦@]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wtt234/article/details/124344127
边栏推荐
- Analysis and configuration process of epit periodic timer for imx6ull bare metal development
- Android development interview questions collection and sorting (including answers), [work perception]
- DCB "first brother" xianruida, what does it rely on to raise the value curve?
- [HCTF 2018]admin
- DCB“一哥”先瑞达,靠什么拉升价值曲线?
- The code bought from the salted fish | SSM hotel room management system | whether the purchased source code can really be used | confusion from the programmer | Yu Nianyu Hui | Wu Minghui, dachou Vill
- Here's the point. Have you mastered the most complete Web3 jargon guide?
- io_ Application of ring technology in distributed cloud native database
- Amazon Aurora's ability to read and write: shardingsphere proxy
- Live broadcast software | IPTV live broadcast software | TV live broadcast | tvplayer IPTV easyplayer | youwo live broadcast | customized development of super live broadcast software
猜你喜欢

Application of safe electricity management platform in Jingbian Museum safe electricity management system

Android development art exploration notes (23), summary of Android advanced interview questions in 2022

为企业出海“搭桥”,汇量科技靠什么出圈?

Change your posture to look at hooks, the combination of inspiration sources and the separation of new ideas from logical view in hoc mode

曦智科技沈亦晨入选2022达沃斯世界经济论坛“全球青年领袖”

Acrel-5000型建筑能耗监测系统在西咸空港花园项目的研究与应用

Information system project management - project initiation management

What happens when you run the NPM install command?

Tdengine deployment cluster installation

DCB“一哥”先瑞达,靠什么拉升价值曲线?
随机推荐
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
Interface automation session authentication solution
你真的会用K折交叉吗? 对于K折交叉的思考
Design and application of intelligent lighting control system in hospital
Tdengine deployment cluster installation
Analysis and configuration process of epit periodic timer for imx6ull bare metal development
Cross domain problem and solving cross domain problem by UMI proxy proxy
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
DCB“一哥”先瑞达,靠什么拉升价值曲线?
Application of safe electricity management platform in Jingbian Museum safe electricity management system
Articles for May
Essay 8: error in readpng (paste (location, "/", pattern. Type [i], ". PNG", Sep = "): unable to open C:/
Originally, this is the correct posture for developers to open world book day!
简单聊聊Ruby
10月24号是什么?中国的高级程序员为什么没有五六十岁的,而国外人、、、,玉念聿辉带你过1024程序员节日
Android development art exploration notes (23), summary of Android advanced interview questions in 2022
光猫超级帐号密码,重置光猫获取超级帐号密码
Mobile Jingdong Mall
What kind of project is suitable for automated testing?
App uses the template message from WeChat official account for message push.