package chapter11
import (
"fmt"
"gin_project/models"
"github.com/gin-gonic/gin"
"net/http"
)
type Book struct {
Id int `json:"id"`
Name string `json:"name"`
Author string `json:"author"`
}
func ApiTest(ctx *gin.Context) {
// 结构体 map
student := models.Student{Id: 1,Name: "林锡宏",Age: 33}
// 数组 切片
arrs := []int{1,2,3,4,5,6}
// 结构体数组
arr_struct := []models.Student{
{Id: 1,Name: "林",Age: 23},
{Id: 2,Name: "锡",Age: 30},
{Id: 3,Name: "宏",Age: 33},
}
// 结构体map
map_struct := map[string]models.Student{
"student":models.Student{Id: 4,Name: "林正伍",Age: 2},
}
ctx.JSON(http.StatusOK,gin.H{
"code":http.StatusOK,
"message":"成功",
"student":student,
"arrs":arrs,
"arr_struct":arr_struct,
"map_struct":map_struct,
})
}
// get 接受前端值
func GetBook(ctx *gin.Context) {
books := []Book{
{Id: 1,Name: "西游记",Author: "吴承恩"},
{Id: 2,Name: "水浒传",Author: "施耐庵"},
{Id: 3,Name: "红楼梦",Author: "曹雪芹"},
{Id: 4,Name: "三国演义",Author: "罗贯中"},
}
ctx.JSON(http.StatusOK,gin.H{
"books":books,
})
}
// 获取id
func GetBOOKDetail(ctx *gin.Context){
id := ctx.GetString("id")
fmt.Println(id)
}
获取请求参数