gpt4 book ai didi

json - 接口(interface)转换 : interface {} is int64, 不是 []uint8

转载 作者:数据小太阳 更新时间:2023-10-29 03:46:31 26 4
gpt4 key购买 nike

我正在尝试实现一个可以处理 http 请求并在嵌套 JSON 中发送响应的 go 程序。当我运行我的代码并调用 URL 时,出现运行时错误,这是什么意思?我该如何处理?

panic serving 192.168.0.101:50760: interface conversion: interface {} is int64, not []uint8
goroutine 5 [running]

这是我的函数代码,它在点击 url 时被调用

func logInPass(res http.ResponseWriter, req *http.Request) {
type Resp struct {
Result []map[string]interface{} `json:"Result,omitempty"`
Status string `json:"Status"`
}
type AxleUser struct {
Mobile string `json:"Mobile"`
Password string `json:"Password"`
}

var Response Resp
Response.Status = "failed"
Result := make(map[string]interface{})
db, err := sql.Open("mysql", "root:chikkIbuddI57@tcp(127.0.0.1:3306)/b2b")
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())
}
// get shop id from emp table using mobile number and password
userrows, usererr := db.Query("SELECT b2b_emp_id,b2b_shop_id,b2b_shop_name,b2b_emp_name,b2b_emp_mobile_number FROM b2b_employee_tbl WHERE b2b_emp_mobile_number=? and b2b_password=?", msg.Mobile, msg.Password)
if usererr != nil {
panic(usererr.Error())
}
usercolumns, usererr := userrows.Columns()
if usererr != nil {
panic(usererr.Error())
}

usercount := len(usercolumns)
values := make([]interface{}, usercount)
scanArgs := make([]interface{}, usercount)
for i := range values {
scanArgs[i] = &values[i]
}
for userrows.Next() {
usererr := userrows.Scan(scanArgs...)
if usererr != nil {
panic(usererr.Error())
}
for i, v := range values {
if v != nil {
Result[usercolumns[i]] = fmt.Sprintf("%s", string(v.([]byte)))
}
}
Response.Result = append(Response.Result, Result)
Response.Status = "success"
}

res.Header().Set("Content-Type", "application/json")
rnd.JSON(res, http.StatusOK, Response)
}

提前致谢!

最佳答案

我改变了这一行

values := make([]interface{}, usercount)

values := make([]string, usercount)

还有这一行

Result[usercolumns[i]] = fmt.Sprintf("%s", string(v.([]byte)))

Result[usercolumns[i]] = v

关于json - 接口(interface)转换 : interface {} is int64, 不是 []uint8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49068556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com