首页 > 其他分享 >初识Gin

初识Gin

时间:2022-10-12 13:12:01浏览次数:46  
标签:200 context JSON 初识 func gin Gin hello

Gin

都说学一门技术,先用就完事了,所以我也是这样想的。

获取Gin

go get github.com/gin-gonic/gin

Gin是支持RESTFUL风格的请求的

直接上实例

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	// RESTFUL 风格开发
	r.GET("/hello", func(context *gin.Context) {
		//context.Writer.Write([]byte("hello world"))
		context.JSON(200, gin.H{
			"code":    "200",
			"message": "hello, gin",
		})
	})
	r.PUT("/hello", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"code":    200,
			"message": "你使用了put方法",
		})
	})
	r.DELETE("/hello", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"code":    200,
			"message": "你使用了DELETE",
		})
	})
	r.POST("/hello", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"code":    200,
			"message": "你使用了POST",
		})
	})
	r.Run()
}

参数绑定

由于HTTP请求中有各种不同的请求类型,我们想兼容这些请求类型,所以做了一个类型处理进行参数绑定
首先要创建一个结构体去兼容所有的信息

type Login struct {
	username string `json:"username", form:"username", binding:"username"`
	password string `json:"password", form:"password", binding:"password"`
}

有了这个结构体以后,我们就能用反射把不同类型的值映射到我们的类型变量上了。

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Login struct {
	Username string `json:"username" form:"username" binding:"required"`
	Password string `json:"password" form:"password" binding:"required"`
}

func main() {
	r := gin.Default()
	// RESTFUL 风格开发
	r.GET("/hello", func(context *gin.Context) {
		var login Login
		if err := context.ShouldBind(&login); err != nil {
			context.JSON(http.StatusBadRequest, gin.H{
				"err": err.Error(),
			})
		} else {
			context.JSON(200, gin.H{
				"username": login.Username,
				"password": login.Password,
			})
		}
	})
	r.DELETE("/hello", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"code":    200,
			"message": "你使用了DELETE",
		})
	})
	r.POST("/hello", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"code":    200,
			"message": "你使用了POST",
		})
	})
	r.Run()
}

标签:200,context,JSON,初识,func,gin,Gin,hello
From: https://www.cnblogs.com/azxx/p/16784176.html

相关文章

  • 初识Shell脚本
    1简介SHELL是UNIX系统的用户与操作系统之间的一种接口。它既是UNIX系统的命令解释程序,又是一种高级的命令程序设计语言。作为命令解释程序,SHELL接收用户输入的命令,将命......
  • 了解 EventLog Analyzer Manager Engine 的 7 个最重要的功能
    EventLogAnalyzer是一种便捷的信息安全和事件管理(SIEM)解决方案,有助于提高网络安全性并满足审计要求。EventLogAnalyzer该程序还能够收集、分析、搜索、报告和存档从各......
  • nginx配置——根据路由参数来设置对应响应方式
      location/{set$is_matched0;#是否有匹配的参数#正则判断url中携带的参数是否有匹配if($query_string~".*(?:^|\?|&)token=123"){set$is_ma......
  • 初识设计模式 - 享元模式
    简介古代的活字印刷术就有点像享元模式,活字印刷就是将每个字模做出来,再印刷时再选取需要的字模到印刷板上,这样就构成了一页书的印刷板。这样的活字印刷大大提升了效率,减少......
  • CENTOS安装NGINX报错
    1特别是在CentOS7上安装Nginx,不确定你有什么样的错误(无法打开你的图像/图片),但刚刚在CentOS7上尝试过(大约10分钟前)(VagrantBox"CentOS-7.2-1.8T"),它通过......
  • JavaScript works behind the scenes -- Engine and Runtime
    whatisaJavaScriptengine?programthatexecutesJavaScriptcode.JavaScript引擎是运行JavaScript代码的程序。howengineworks?JavaScriptcontainsacallsta......
  • nginx负载均衡策略
    目前Nginx服务器的upstream模块支持6种方式的分配:轮询默认方式weight权重方式ip_hash依据ip分配方式least_conn最少连接方式fair(第三方)响应时间方......
  • centos7 安装 Nginx 步骤
    1安装gcc2安装perl库yuminstall-ypcrepcre-devel3安装zlib库yuminstall-yzlibzlib-devel4先解压,然后到文件夹里面./configure--prefix=/usr/local/nginx......
  • [翻译] Quorum Queues - Making RabbitMQ More Competitive In Reliable Messaging
    目录AnIntroductiontoRaftBriefOverviewofRaftWriteSafetyReadSafetyRabbitMQQuorumQueuesNewDrawbacksQuestionsandOpenTopicsCouldtheidempotencyofwr......
  • 关于red hat linux的login和passwd都忘记解决方法(在学校机房)
    忘记账号的参考文档:linux系统忘记用户名和登录密码怎么办-简书(jianshu.com);忘记密码的参考文档:(88条消息)Linux忘记密码解决方法——RedHat_(YSY_YSY)的博客-CSDN博客......