var _ Interface = (*Struct)(nil)

检测类型是否实现接口

package main

type color struct {
	r, g, b, a int
}

func (c *color) GetRGBA() (int, int, int, int) {
	return c.r, c.g, c.b, c.a
}

type Color interface {
	GetRGBA() (int, int, int, int)
}

var _ Color = (*color)(nil)

https://segmentfault.com/a/1190000022255009#item-3-4

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
改进代码func TracingList(ctx *gin.Context) { resp := models.Response{ Code: 0, Msg: "success", } t, _ := strconv.Atoi(ctx.Query("t")) label, _ := strconv.Atoi(ctx.Query("label")) page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10")) code := ctx.Query("code") //设施类型 if len(code) == 0 { resp.Code = 400 resp.Msg = "请输入code值" ctx.JSON(400, resp) return } type Total struct { gorm.Model Type int16 Source int16 Explanation string Label int16 FacilityID string } db := common.DB.Session(&gorm.Session{}) if t < 0 || t > 5 { var req interface{} switch t { case 0: req = []*Total{} case 1: req = []*models.Flaw{} case 2: req = []*models.Issue{} case 3: req = []*models.Record{} case 4: req = []*models.Quality{} case 5: req = []*models.Inspection{} } //查询并分页 if t < 1 || t > 5 { res := db.Scopes(models.Paginate(page, pageSize)).Where("facility_id=?", code).Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } } else { res := db.Scopes(models.Paginate(page, pageSize)).Raw("SELECT * FROM flaws WHERE facility_id = ? "+ "UNION SELECT * FROM issues WHERE facility_id = ? "+ "UNION SELECT * FROM records WHERE facility_id = ? "+ "UNION SELECT * FROM qualities WHERE facility_id = ? "+ "UNION SELECT * FROM inspections WHERE facility_id = ?", "code", "code", "code", "code", "code").Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } } resp.Data = req ctx.JSON(200, resp) } else if label < 1 || label > 4 { var req []*Total res := db.Scopes(models.Paginate(page, pageSize)).Raw("SELECT * FROM flaws WHERE label = ? "+ "UNION SELECT * FROM issues WHERE label = ? "+ "UNION SELECT * FROM records WHERE label = ? "+ "UNION SELECT * FROM qualities WHERE label = ? "+ "UNION SELECT * FROM inspections WHERE label = ?", "label", "label", "label", "label", "label").Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } resp.Data = req ctx.JSON(200, resp) } else { ctx.JSON(400, models.Response{ Code: 400, Msg: "参数有误", }) return } }
05-26
以下是改进的代码: ```go func TracingList(ctx *gin.Context) { resp := models.Response{ Code: 0, Msg: "success", } code := ctx.Query("code") if len(code) == 0 { resp.Code = 400 resp.Msg = "请输入code值" ctx.JSON(400, resp) return } t, err := strconv.Atoi(ctx.Query("t")) if err != nil || t < 0 || t > 5 { resp.Code = 400 resp.Msg = "无效的参数t" ctx.JSON(400, resp) return } label, err := strconv.Atoi(ctx.Query("label")) if err != nil || label < 1 || label > 4 { resp.Code = 400 resp.Msg = "无效的参数label" ctx.JSON(400, resp) return } page, err := strconv.Atoi(ctx.DefaultQuery("page", "1")) if err != nil || page < 1 { resp.Code = 400 resp.Msg = "无效的参数page" ctx.JSON(400, resp) return } pageSize, err := strconv.Atoi(ctx.DefaultQuery("pageSize", "10")) if err != nil || pageSize < 1 { resp.Code = 400 resp.Msg = "无效的参数pageSize" ctx.JSON(400, resp) return } var req interface{} switch t { case 0: req = []*Total{} case 1: req = []*models.Flaw{} case 2: req = []*models.Issue{} case 3: req = []*models.Record{} case 4: req = []*models.Quality{} case 5: req = []*models.Inspection{} } db := common.DB.Session(&gorm.Session{}) res := db.Scopes(models.Paginate(page, pageSize)).Where("facility_id=?", code) if t >= 1 && t <= 5 { res = res.Table("flaws").Where("facility_id=?", code). Or("facility_id=?", code). Or("facility_id=?", code). Or("facility_id=?", code). Or("facility_id=?", code) } res.Find(req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } resp.Data = req ctx.JSON(200, resp) } ``` 主要改进包括: 1. 对输入的参数进行了有效性检查,如 t、label、page 和 pageSize 都需要为正整数,code 不能为空。 2. 使用了 Go 语言内置的错误处理机制,当参数无效时,返回错误信息。 3. 将 SQL 查询语句拆分为两段,根据 t 的值判断使用哪一段,避免了使用 Raw() 方法的安全隐患。 4. 代码结构更加清晰,易于阅读和维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

metabit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值