查询语句:
```
rows, err := mysqlDB.Query("SELECT * FROM `article` where `id` = ? ", 32);
```
相应函数为:
```
// Query via pool
func (p *SQLConnPool) Query(queryStr string, args ...interface{}) ([]map[string]interface{}, error) {
rows, err := p.SQLDB.Query(queryStr, args...)
if err != nil {
log.Println(err)
return []map[string]interface{}{}, err
}
defer rows.Close()
columns, _ := rows.Columns()
scanArgs := make([]interface{}, len(columns))
values := make([]interface{}, len(columns))
for i := range values {
scanArgs[i] = &values[i]
}
rowsMap := make([]map[string]interface{}, 0, 10)
for rows.Next() {
//将行数据保存到record字典
err = rows.Scan(scanArgs...)
rowMap := make(map[string]interface{})
for i, col := range values {
if col != nil {
rowMap[columns[i]] = string(col.([]byte))
}
}
rowsMap = append(rowsMap, rowMap)
}
if err = rows.Err(); err != nil {
return []map[string]interface{}{}, err
}
return rowsMap, nil
}
```
报错信息:
```
interface conversion: interface {} is int64, not []uint8
/usr/local/go/src/runtime/panic.go:491 (0x42b512)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/local/go/src/runtime/iface.go:172 (0x40f9b6)
panicdottypeE: panic(&TypeAssertionError{iface.string(), haveString, want.string(), ""})
/root/go/src/github.com/fecshopsoft/golang-db/mysql/mysql.go:74 (0x92cda2)
(*SQLConnPool).Query: rowMap[columns[i]] = string(col.([]byte))
/root/go/src/github.com/fecshop/go_fec_api/handler/article.go:54 (0x92e475)
ArticleData.OneByUrlKey: _, _ = mysqlDB.Query("SELECT * FROM `article` where `id` = ? ", 32);
/root/go/src/github.com/fecshop/go_fec_api/router/router.go:42 (0x92fba3)
Begin.func2: handler.Article.OneByUrlKey(c, mysqlDB)
/root/go/src/github.com/gin-gonic/gin/context.go:107 (0x8e2812)
(*Context).Next: c.handlers[c.index](c)
/root/go/src/github.com/gin-gonic/gin/recovery.go:46 (0x8f4ab9)
RecoveryWithWriter.func1: c.Next()
/root/go/src/github.com/gin-gonic/gin/context.go:107 (0x8e2812)
(*Context).Next: c.handlers[c.index](c)
/root/go/src/github.com/gin-gonic/gin/logger.go:83 (0x8f3deb)
LoggerWithWriter.func1: c.Next()
/root/go/src/github.com/gin-gonic/gin/context.go:107 (0x8e2812)
(*Context).Next: c.handlers[c.index](c)
/root/go/src/github.com/gin-gonic/gin/gin.go:359 (0x8eb925)
(*Engine).handleHTTPRequest: c.Next()
/root/go/src/github.com/gin-gonic/gin/gin.go:326 (0x8eb0ba)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/go/src/net/http/server.go:2619 (0x664903)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/go/src/net/http/server.go:1801 (0x660a4c)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/go/src/runtime/asm_amd64.s:2337 (0x459790)
goexit: BYTE $0x90 // NOP
```
看报错信息明白个大概,但是不知道如何改,来消除报错。
有疑问加站长微信联系(非本文作者)