优化代码 var modelMap = map[string]interface{}{ "comb": &models.PsComb{}, "conduit": &models.PsConduit{}, "feature": &models.Feature{}, "discharger": &models.PsDischarger{}, "gate": &models.PsGate{}, "interception": &models.PsInterception{}, "lake": &models.PsLake{}, "manhole": &models.PsManhole{}, "outfall": &models.PsOutfall{}, "pipe": &models.PsPipe{}, "pumpStation": &models.PsPumpStation{}, "retention": &models.PsRetention{}, "river": &models.PsRiver{}, "wwtp": &models.PsWWTP{}, "psSystem": &models.PsSystem{}, "landUse": &models.LandUse{}, "publicity": &models.Publicity{}, } func Batch(ctx *gin.Context) { var requ Request if err := ctx.ShouldBind(&requ); err != nil { ctx.JSON(400, models.Response{ Code: 400, Msg: "参数有误", Data: err.Error(), }) return } resp := models.Response{ Code: 0, Msg: "success", } for _, item := range requ.Post { if reqType, ok := modelMap[item.Type]; ok { if err := mapstructure.Decode(item.Value, reqType); err != nil { panic(err) } res := common.DB.Create(reqType) if res.Error != nil { resp.Code = 500 resp.Msg = "创建失败" resp.Data = res.Error ctx.JSON(500, resp) return } ctx.JSON(200, resp)