栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Gin框架2

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gin框架2

1.补充1中的实验过程: (1)
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

func main() {
	engine := gin.Default()
	//http://localhost:8080/hello?name=davie
	engine.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//login
	engine.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 main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

func main() {
	engine := gin.Default()

	//http://localhost:8080/hello?name=davie
	engine.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 main

import (
	"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 Student
		err := 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 main

import (
	"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 Register
		if 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 main

import (
	"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 main

import (
	"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    int
	Message string
	Data    interface{}
}

6.Group(路由组)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731655.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号