记录go语言使用var和:= 的小区别

直接看代码

func main(){
var ff []int
f:=[]int{}
fmt.Println(ff==nil) // true
fmt.Println(len(ff),cap(ff)) //0 0
fmt.Println(f==nil) //false
fmt.Println(len(f),cap(f)) //0 0
}

输出:

true
0 0
0 0
false
改进代码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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值