欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Gin框架2

时间:2023-07-09
1.补充1中的实验过程: (1)

package mainimport ("fmt""github.com/gin-gonic/gin")func main() {engine := gin.Default()//http://localhost:8080/hello?name=davieengine.Handle("GET", "/hello", func(context *gin.Context) {path := context.FullPath()fmt.Println(path)name := context.DefaultQuery("name", "hello")fmt.Println(name)//输出,返回给前端context.Writer.Write([]byte("Hello ," + name))})//post//http://localhost:8080//loginengine.Handle("POST", "/login", func(context *gin.Context) {fmt.Println(context.FullPath())userename := context.PostForm("username")password := context.PostForm("password")fmt.Println(userename)fmt.Println(password)context.Writer.Write([]byte(userename + "登录"))})engine.Run()}

使用postman输入:

 终端打印:

(2)delete

package mainimport ("fmt""github.com/gin-gonic/gin")func main() {engine := gin.Default()//http://localhost:8080/hello?name=davieengine.GET("/hello", func(context *gin.Context) {fmt.Println(context.FullPath())name := context.Query("name")fmt.Println(name)context.Writer.Write([]byte("Hello," + name))})engine.POST("/login,", func(context *gin.Context) {fmt.Println(context.FullPath())username, exist := context.GetPostForm("username")if exist {fmt.Println(username)}password, exist := context.GetPostForm("password")if exist {fmt.Println(password)}context.Writer.Write([]byte("Hello" + username))})engine.DELETE("/user/:id", func(context *gin.Context) {userID := context.Param("id")context.Writer.Write([]byte("delete 用户ID:" + userID))})engine.Run()}

postman:

2.参数绑定

package mainimport ("fmt""github.com/gin-gonic/gin""log")func main() {engine := gin.Default()// http://localhost:8080/hello?name=davie&classes=计算机engine.GET("/hello", func(context *gin.Context) {fmt.Println(context.FullPath())var student Studenterr := context.ShouldBindQuery(&student)if err != nil {log.Fatal(err.Error())return}fmt.Println(student.Name)fmt.Println(student.Classes)context.Writer.Write([]byte("hello," + student.Name))})engine.Run()}type Student struct {Name string `form:"name"`Classes string `form:"classes"`}

网站输入:

http://localhost:8080/hello?name=curry&classes=计算机

得到:

 终端得到:

 网站输入:

http://localhost:8080/hello?name=davie&classes=软件工程

得到:

终端得到:

 
3.注册绑定

package mainimport ("fmt""github.com/gin-gonic/gin""log")func main() {engine := gin.Default()engine.POST("/register", func(context *gin.Context) {fmt.Println(context.FullPath())var register Registerif err := context.ShouldBind(®ister); err != nil {log.Fatal(err.Error())return}fmt.Println(register.UserName)fmt.Println(register.Phone)context.Writer.Write([]byte(register.UserName + " Register"))})engine.Run()}type Register struct {UserName string `form:"username"` //绑定Phone string `form:"phone"`Password string `form:"password"`}

postman:

 4.[]byte格式

package mainimport ("fmt""github.com/gin-gonic/gin")func main() {engine := gin.Default()engine.GET("/hellobyte", func(context *gin.Context) {fullpath := "请求路径:" + context.FullPath()fmt.Println(fullpath)context.Writer.Write([]byte(fullpath))})engine.GET("/hellostring", func(context *gin.Context) {fullpath := "请求路径:" + context.FullPath()fmt.Println(fullpath)context.Writer.WriteString(fullpath)})engine.Run()}

 

5.JSon格式

package mainimport ("fmt""github.com/gin-gonic/gin")func main() {engine := gin.Default()engine.GET("/hellojson", func(context *gin.Context) {fullpath := "请求路径:" + context.FullPath()fmt.Println(fullpath)context.JSON(200, map[string]interface{}{"code": 1,"message": "OK","data": fullpath,})})engine.GET("/jsonstruct", func(context *gin.Context) {fullpath := "请求路径:" + context.FullPath()fmt.Println(fullpath)resp := Response{Code: 1, Message: "ok", data: fullpath}context.JSON(200, &resp)})engine.Run()}type Response struct {Code intMessage stringData interface{}}

6.Group(路由组)

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。