package main
import (
"fmt"
"log"
"strconv"
"github.com/gin-gonic/gin"
)
type User struct {
Uid int `json:"uid"`
Name string `json:"name"`
Age int `json:"age"`
}
var users = make([]User, 3)
func init() {
u1 := User{1, "Psych", 18}
u2 := User{2, "Morax", 1000}
u3 := User{3, "Cyno", 16}
users = append(users, u1)
users = append(users, u2)
users = append(users, u3)
fmt.Println(users)
}
func find(uid int) (*User, int) {
for i, u := range users {
if u.Uid == uid {
return &u, i
}
}
return nil, -1
}
func AddUser(c *gin.Context) {
u4 := User{4, "Klee", 10}
users = append(users, u4)
c.JSON(200, users)
}
func DelUser(c *gin.Context) {
uid := c.Param("uid")
id, err := strconv.Atoi(uid)
if err != nil {
log.Fatal(err)
}
_, i := find(id)
users = append(users[:i], users[i+1:]...)
c.JSON(200, users)
}
func UpdateUser(c *gin.Context) {
uid := c.Param("uid")
id, err := strconv.Atoi(uid)
if err != nil {
log.Fatal(err)
}
u, _ := find(id)
u.Name = "修改的Name"
c.JSON(200, u)
}
func FindUser(c *gin.Context) {
uid := c.Param("uid")
id, err := strconv.Atoi(uid)
if err != nil {
log.Fatal(err)
}
u, _ := find(id)
c.JSON(200, u)
}
func main() {
e := gin.Default()
e.GET("/user/:uid", FindUser)
e.PUT("/user/:uid", UpdateUser)
e.DELETE("/user/:uid", DelUser)
e.POST("/user/", AddUser)
e.Run()
}
使用软件Postman
进行测试:
查找:
更新:
删除:
添加: