这是我的go功能
func logInFirst(res http.ResponseWriter, req *http.Request) {
type Resp struct {
Result []map[string]interface{} `json:"Result,omitempty"`
Result1 []map[string]interface{} `json:"Result1,omitempty"`
Result2 []map[string]interface{} `json:"Result2,omitempty"`
Status string `json:"Status"`
}
type AxleUser struct {
ShopID string `json:"ShopID"`
VehicleType string `json:"VehicleType"`
}
var Response Resp
Response.Status = "failed"
Result := make(map[string]interface{})
Result1 := make(map[string]interface{})
Result2 := make(map[string]interface{})
//db, err := sql.Open("mysql", "root:chikkIbuddI57@tcp(127.0.0.1:3306)/b2b")
db, err := sql.Open("mysql", "awsgobumpr:gobumpr123@tcp(briaxpgbmpr.cx4twoxoumct.ap-southeast-1.rds.amazonaws.com)/b2b_optimization")
if err != nil {
panic(err.Error())
}
defer db.Close()
rnd := render.New()
b, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if err != nil {
panic(err.Error())
}
// Unmarshal the request body
var msg AxleUser
err = json.Unmarshal(b, &msg)
if err != nil {
panic(err.Error())
}
//fmt.Println(msg)
// get shop id from emp table using mobile number and password
brandrows, branderr := db.Query("SELECT DISTINCT brand,model FROM admin_vehicle_table_new WHERE type=?", msg.VehicleType)
if branderr != nil {
panic(branderr.Error())
}
brandcolumns, branderr := brandrows.Columns()
if branderr != nil {
panic(branderr.Error())
}
brandcount := len(brandcolumns)
brandValues := make([]string, brandcount)
brandScanArgs := make([]interface{}, brandcount)
for i := range brandValues {
brandScanArgs[i] = &brandValues[i]
}
for brandrows.Next() {
branderr := brandrows.Scan(brandScanArgs...)
//fmt.Println(branderr)
if branderr != nil {
panic(branderr.Error())
}
for i, v := range brandValues {
Result[brandcolumns[i]] = v
}
Response.Result = append(Response.Result, Result)
}
servicerows, serviceerr := db.Query("SELECT DISTINCT b2b_service_type FROM b2b_service_type WHERE b2b_vehicle_type=? and b2b_flag=0 and b2b_shop_id=0 UNION SELECT b2b_service_type FROM b2b_service_type WHERE b2b_vehicle_type=? and b2b_flag=0 and b2b_shop_id=?", msg.VehicleType, msg.VehicleType, msg.ShopID)
if serviceerr != nil {
panic(serviceerr.Error())
}
servicecolumns, serviceerr := servicerows.Columns()
if serviceerr != nil {
panic(serviceerr.Error())
}
servicecount := len(servicecolumns)
serviceValues := make([]string, servicecount)
serviceScanArgs := make([]interface{}, servicecount)
for i := range serviceValues {
serviceScanArgs[i] = &serviceValues[i]
}
for servicerows.Next() {
serviceerr := servicerows.Scan(serviceScanArgs...)
if serviceerr != nil {
panic(serviceerr.Error())
}
for i, v := range serviceValues {
Result1[servicecolumns[i]] = v
}
Response.Result1 = append(Response.Result1, Result1)
}
repairrows, repairerr := db.Query("SELECT DISTINCT b2b_repair_decription FROM b2b_repair_decription WHERE b2b_vehicle_type=? and b2b_shop_id=0 UNION SELECT b2b_repair_decription FROM b2b_repair_decription WHERE b2b_vehicle_type=? and b2b_shop_id=?", msg.VehicleType, msg.VehicleType, msg.ShopID)
if repairerr != nil {
panic(repairerr.Error())
}
repaircolumns, repairerr := repairrows.Columns()
if repairerr != nil {
panic(repairerr.Error())
}
repaircount := len(repaircolumns)
repairValues := make([]string, repaircount)
repairScanArgs := make([]interface{}, repaircount)
for i := range repairValues {
repairScanArgs[i] = &repairValues[i]
}
for repairrows.Next() {
repairerr := repairrows.Scan(repairScanArgs...)
if repairerr != nil {
panic(repairerr.Error())
}
for i, v := range repairValues {
Result2[repaircolumns[i]] = v
}
Response.Result2 = append(Response.Result2, Result2)
}
Response.Status = "success"
res.Header().Set("Content-Type", "application/json")
rnd.JSON(res, http.StatusOK, Response)
}
我的Desrired输出是一个像这样的json字符串
{
"Result": [
{
"brand": "Hero Honda",
"model": "Passion Pro"
},
{
"brand": "Yamaha",
"model": "120"
},...
]
"Result1":[
{
"service_type" : "repairs",
},
{
"service_type" : "general service",
},...
]
"Result2":[
{
"b2b_repair_decription": "Tire Replacement"
},
{
"b2b_repair_decription": "Electric work"
},...
]
}
但我得到的输出包含相同的行而不是所有的行 此,
{
"Result": [
{
"brand": "Yamaha",
"model": "120"
},
{
"brand": "Yamaha",
"model": "120"
},...
]
"Result1":[
{
"service_type" : "general service",
},
{
"service_type" : "general service",
},...
]
"Result2":[
{
"b2b_repair_decription": "Electric work"
},
{
"b2b_repair_decription": "Electric work"
},...
]
}
我尝试在循环中追加,但它有效。我甚至尝试将Result,Result1,Result2定义为接口数组
有人可以帮我解决这个问题吗?