前言

代码参考自《Building Distributed Application in Gin》

需求:设计一个食谱相关的API,数据存放到切片中。

设计模型和API

模型

type Recipe struct {
// 菜品名
Name string `json:"name"`
// 菜品标签,字符串切片
Tags []string `json:"tags"`
// 菜品材料
Ingredients []string `json:"ingredients"`
// 制作步骤
Instructions []string `json:"instructions"`
// 发布时间
PublishedAt time.Time `json:"publishedAt"`
}

API

http method resource description
GET /recipes 返回一列recipe数据
POST /recipes 创建新食谱
PUT /recipes/{id} 更新一个已存在的食谱
DELETE /recipes/{id} 删除一个已存在的食谱
GET /recipes/search?tag=X 根据标签查询食谱

实现接口

添加一个新食谱

  • POST /recipes
package main

import (
"net/http"
"strings"
"time" "github.com/gin-gonic/gin"
"github.com/rs/xid"
) type Recipe struct {
ID string `json:"id"`
Name string `json:"name"`
Tags []string `json:"tags"`
Ingredients []string `json:"ingredients"`
Instructions []string `json:"instructions"`
PublishedAt time.Time `json:"publishedAt"`
} // 保存recipes数据
var recipes []Recipe func init() {
recipes = make([]Recipe, 0)
} func NewRecipeHandler(c *gin.Context) {
// POST /recipes。创建新食谱
var recipe Recipe
if err := c.ShouldBindJSON(&recipe); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
} recipe.ID = xid.New().String()
recipe.PublishedAt = time.Now()
recipes = append(recipes, recipe)
c.JSON(http.StatusOK, recipe)
} func main(){
router := gin.Default()
    router.POST("/recipes", NewRecipeHandler)
    router.Run("127.0.0.1:8080")
}
  • 使用python进行测试
import requests
import json def post_test(data):
url = "http://127.0.0.1:8080/recipes"
resp = requests.post(url=url, data=json.dumps(data))
print("post test")
print(resp.text) if __name__ == "__main__":
data1 = {
"name": "Homemade Pizza",
"tags": ["italian", "pizza", "dinner"],
"ingredients": [
"1 1/2 cups (355 ml) warm water (105°F-115°F)",
"1 package (2 1/4 teaspoons) of active dry yeast",
"3 3/4 cups (490 g) bread flour",
"feta cheese, firm mozzarella cheese, grated",
],
"instructions": [
"Step 1.",
"Step 2.",
"Step 3.",
],
}
post_test(data1)

获取所有食谱

  • GET /recipes
func ListRecipesHandler(c *gin.Context) {
c.JSON(http.StatusOK, recipes)
} func main(){
router.GET("/recipes", ListRecipesHandler)
}
  • python测试
def get_test():
url = "http://127.0.0.1:8080/recipes"
resp = requests.get(url)
print("get test")
print(resp.text) get_test()

根据食谱id更新食谱

func UpdateRecipeHandler(c *gin.Context) {
// PUT /recipes/:id
id := c.Param("id")
var recipe Recipe
if err := c.ShouldBindJSON(&recipe); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
} index := -1
// 根据id遍历查询
for i := 0; i < len(recipes); i++ {
if recipes[i].ID == id {
index = i
}
} if index == -1 {
c.JSON(http.StatusNotFound, gin.H{
"error": "Recipe Not Found",
})
return
} recipe.ID = id
recipes[index] = recipe
c.JSON(http.StatusOK, recipe)
} func main(){
router.PUT("/recipes/:id", UpdateRecipeHandler)
}
  • python测试
def put_test(data, id):
url = f"http://127.0.0.1:8080/recipes/{id}"
# data["id"] = id
resp = requests.put(url=url, data=json.dumps(data))
print("put test")
print(resp.text) data2 = {
"name": "西红柿炒鸡蛋",
"tags": ["家常菜", "新手必会"],
"ingredients": [
"2个鸡蛋",
"1个番茄, 切片",
"葱花, 蒜瓣等",
],
"instructions": [
"步骤1",
"步骤2",
"步骤3",
],
} put_test(data2, id="someidstring")

根据id删除食谱

func DeleteRecipeHandler(c *gin.Context) {
// DELETE /recipes/:id
id := c.Param("id")
index := -1
// 根据id遍历查询
for i := 0; i < len(recipes); i++ {
if recipes[i].ID == id {
index = i
}
} if index == -1 {
c.JSON(http.StatusNotFound, gin.H{
"error": "Recipe Not Found",
})
return
} // 通过切片切割实现删除元素
recipes = append(recipes[:index], recipes[index+1:]...) c.JSON(http.StatusOK, gin.H{
"message": "Recipe has been deleted",
})
} func main(){
router.DELETE("/recipes/:id", DeleteRecipeHandler)
}
  • python测试
def delete_test(id):
url = f"http://127.0.0.1:8080/recipes/{id}"
resp = requests.delete(url=url)
print("delete test")
print(resp.text) delete_test(id="someIdString")

根据标签搜索食谱

func SearchRecipeHandler(c *gin.Context) {
// GET /recipes/search
tag := c.Query("tag")
listOfRecipes := make([]Recipe, 0) for i := 0; i < len(recipes); i++ {
found := false
for _, t := range recipes[i].Tags {
if strings.EqualFold(t, tag) {
found = true
}
} if found {
listOfRecipes = append(listOfRecipes, recipes[i])
}
} c.JSON(http.StatusOK, listOfRecipes)
} func main(){
router.GET("/recipes/search", SearchRecipeHandler)
}
  • python测试
def get_test_search(tag):
url = f"http://127.0.0.1:8080/recipes/search?tag={tag}"
resp = requests.get(url=url)
print("get test search")
print(resp.text) get_test_search(tag="家常菜")

完整示例代码

package main

import (
"net/http"
"strings"
"time" "github.com/gin-gonic/gin"
"github.com/rs/xid"
) type Recipe struct {
ID string `json:"id"`
Name string `json:"name"`
Tags []string `json:"tags"`
Ingredients []string `json:"ingredients"`
Instructions []string `json:"instructions"`
PublishedAt time.Time `json:"publishedAt"`
} // 保存recipes
var recipes []Recipe func init() {
recipes = make([]Recipe, 0)
} func NewRecipeHandler(c *gin.Context) {
// POST /recipes。创建新食谱
var recipe Recipe
if err := c.ShouldBindJSON(&recipe); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
} recipe.ID = xid.New().String()
recipe.PublishedAt = time.Now()
recipes = append(recipes, recipe)
c.JSON(http.StatusOK, recipe)
} func ListRecipesHandler(c *gin.Context) {
c.JSON(http.StatusOK, recipes)
} func UpdateRecipeHandler(c *gin.Context) {
// PUT /recipes/:id
id := c.Param("id")
var recipe Recipe
if err := c.ShouldBindJSON(&recipe); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
} index := -1
for i := 0; i < len(recipes); i++ {
if recipes[i].ID == id {
index = i
}
} if index == -1 {
c.JSON(http.StatusNotFound, gin.H{
"error": "Recipe Not Found",
})
return
}
recipe.ID = id
recipes[index] = recipe
c.JSON(http.StatusOK, recipe)
} func DeleteRecipeHandler(c *gin.Context) {
// DELETE /recipes/:id
id := c.Param("id")
index := -1
for i := 0; i < len(recipes); i++ {
if recipes[i].ID == id {
index = i
}
} if index == -1 {
c.JSON(http.StatusNotFound, gin.H{
"error": "Recipe Not Found",
})
return
} recipes = append(recipes[:index], recipes[index+1:]...)
c.JSON(http.StatusOK, gin.H{
"message": "Recipe has been deleted",
})
} func SearchRecipeHandler(c *gin.Context) {
// GET /recipes/search
tag := c.Query("tag")
listOfRecipes := make([]Recipe, 0) for i := 0; i < len(recipes); i++ {
found := false
for _, t := range recipes[i].Tags {
if strings.EqualFold(t, tag) {
found = true
}
} if found {
listOfRecipes = append(listOfRecipes, recipes[i])
}
} c.JSON(http.StatusOK, listOfRecipes)
} func main() {
router := gin.Default()
router.POST("/recipes", NewRecipeHandler)
router.GET("/recipes", ListRecipesHandler)
router.PUT("/recipes/:id", UpdateRecipeHandler)
router.DELETE("/recipes/:id", DeleteRecipeHandler)
router.GET("/recipes/search", SearchRecipeHandler)
router.Run("127.0.0.1:8080")
}

[gin]基于切片实现crud的更多相关文章

  1. 基于mybatis的CRUD

    u  基于Mybatis的CRUD u  掌握MyBatis的结果类型-resultMap和resultType u  掌握MyBatis的参数类型 u  掌握#和$两种语法 1      基于myb ...

  2. SpringMVC 学习笔记(五) 基于RESTful的CRUD

    1.1. 概述 当提交的表单带有_method字段时,通过HiddenHttpMethodFilter 将 POST 请求转换成 DELETE.PUT请求,加上@PathVariable注解从而实现  ...

  3. Mybatis基于SqlSession实现CRUD

    之前我们讲的基于XML还是接口注解配置都是使用接口实现CRUD,本文我们将要讲解通过splsession来实现CRUD,这种方法比较灵活. 基本配置 <!-- spring和MyBatis完美整 ...

  4. [深度学习] 基于切片辅助超推理库SAHI优化小目标识别

    对象检测是迄今为止计算机视觉中最重要的应用领域.然而,小物体的检测和大图像的推理仍然是实际使用中的主要问题,这是因为小目标物体有效特征少,覆盖范围少.小目标物体的定义通常有两种方式.一种是绝对尺度定义 ...

  5. MVC基于Struts2的CRUD,java+bean+struts

    1,所需jar包,将jar包导入lib中 2,项目目录结构 3,struts.xml <?xml version="1.0" encoding="UTF-8&quo ...

  6. 基于SSM的CRUD项目的详解

    创一个maven工程 创建web的目录和web.xml------------右击项目,build projet--->configure  project---->Project fac ...

  7. 基于SSM之Mybatis接口实现增删改查(CRUD)功能

    国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...

  8. 一步步学Mybatis-实现单表情况下的CRUD操作 (3)

    今天这一章要紧接上一讲中的东西,本章中创建基于单表操作的CRUD与GetList操作,此示例中以Visitor表为范例,为了创建一点测试数据我们先弄个Add方法吧 继续在上次的IVisitorOper ...

  9. 第四章 go语言 数组、切片和映射

    文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ 数组是由同构的元素组成.结构体是由异构的元素组成.数据和结构体都是有固定内存大小的数 ...

  10. Go语言类型(布尔、整型、数组、切片、map等)

    1.基本类型 布尔类型:bool 注意:布尔类型不能接受其他类型的赋值,不支持自动或强制的类型转换. 整型:int8.byte(uint8).int16.int.uint.uintptr int.ui ...

随机推荐

  1. 2022-11-07:给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,其中每个节点 至多 有一条出边。 图用一个大小为 n 下标从 0 开始的数组 edges 表示, 节点 i 到

    2022-11-07:给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,其中每个节点 至多 有一条出边. 图用一个大小为 n 下标从 0 开始的数组 edges 表示, 节点 i 到 ...

  2. 2022-08-08:给定一个数组arr,表示从早到晚,依次会出现的导弹的高度。 大炮打导弹的时候,如果一旦大炮定了某个高度去打,那么这个大炮每次打的高度都必须下降一点。 1) 如果只有一个大炮,返回

    2022-08-08:给定一个数组arr,表示从早到晚,依次会出现的导弹的高度. 大炮打导弹的时候,如果一旦大炮定了某个高度去打,那么这个大炮每次打的高度都必须下降一点. (1) 如果只有一个大炮,返 ...

  3. 2022-05-28:某公司计划推出一批投资项目。 product[i] = price 表示第 i 个理财项目的投资金额 price 。 客户在按需投资时,需要遵循以下规则: 客户在首次对项目 pr

    2022-05-28:某公司计划推出一批投资项目. product[i] = price 表示第 i 个理财项目的投资金额 price . 客户在按需投资时,需要遵循以下规则: 客户在首次对项目 pr ...

  4. 2022-01-30:最小好进制。 对于给定的整数 n, 如果n的k(k>=2)进制数的所有数位全为1,则称 k(k>=2)是 n 的一个好进制。 以字符串的形式给出 n, 以字符串的形式返回 n 的

    2022-01-30:最小好进制. 对于给定的整数 n, 如果n的k(k>=2)进制数的所有数位全为1,则称 k(k>=2)是 n 的一个好进制. 以字符串的形式给出 n, 以字符串的形式 ...

  5. PyCharm-汉化、中文语言包、英文语言包、中英文切换

    PyCharm的汉化是非常简单的,不需要繁琐的步骤,只需要到设置的插件中搜索你需要的语言包安装即可. 登录 进入项目(随便进入一个项目,新建也可以) File->settings->Plu ...

  6. 在使用abaqus时可能会遇到的一些问题

    ​我收集了一些网友及客户在使用abaqus软件时遇到的一些问题,下面来看看是如何解决的~ (1)Linux平台使用Abaqus子程序的免费方案 gcc+gfortran 本方法在centos7和cen ...

  7. 使用git 将本地代码上传码市私用仓库操作

    1  现在在登录码市建立项目 2   本地建立一个文件夹,然后使用git bash 3   初始化本地库  git init 4,进入刚刚在码云新建的项目里,复制框框里的路径 5,然后在回到本地新建的 ...

  8. Hbase的JavaAPI和数据存储

    导入Maven依赖 <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> ...

  9. StencilJs学习之事件

    其实并没有所谓的 stencil Event,相反 stencil 鼓励使用 DOM event.然而,Stencil 提供了一个 API 来指定组件可以触发的事件,以及组件监听的事件. 这是通过 E ...

  10. python 星号(*) 还能这么用

    哈喽大家好,我是咸鱼 今天跟大家介绍一下 python 当中星号(*)的一些用法 首先大家最常见的就是在 python 中 * 是乘法运算符,实现乘法 sum = 5 * 5 # 25 除此之外,还有 ...