dqlhsm9820 2019-04-03 15:42
浏览 141
已采纳

在FLOATS上使用GROUP_CONCAT进行SQL查询的应用程序返回[] uint8而不是实际的[] float64

Have a problem using group_concat in a query made by my go application.

Any idea why a group_concat of FLOATS would look like a []uint8 on the Go side?

Cant seem to properly convert the suckers either.

It's definitely floats, I can see it in the raw query results, but when I do the same query in go and try to .Scan the result, Go complains that it's a []uint8 not a []float64 (which it actually is) Attempts to convert to floats gives me the wrong values (and way too many of them).

For example, at the database, I query and get 2 floats for the column in question, looks like this:

"5650.50, 5455.00"

On the go side however, go sees a []uint8 instead of []float64. Why does this happen? How does one workaround this to get the actual results?

My problem is that I have to use this SQL with the group_concat, due to the nature of the database I am working with, this is the best way to get the information, and more importantly the query itself works great, returns the data the function needs, but now I cant read it out because of type issues. No stranger to those, but Go isn't cooperating with me today.

I'd be more than pleased to learn WHY go is doing it this way, and delighted to learn of a way to deal with it.

Example:

SELECT ID, getDistance(33.1543,-110.4353, Loc.Lat, Loc.Lng) as distance,
GROUP_CONCAT(values) FROM stuff INNER JOIN device on device.ID = stuff.ID WHERE (someConditionsETC) GROUP BY ID ORDER BY ID

The actual result, when interfacing with the actual database (not within my application), is

"5650.00, 5850.50"

It's clearly 2 floats.

The same result produces a slice of uint8 when queried from Go and trying to .Scan the result in. If I range through and print those values, I get way more than 2, and they are uint8 (bytes) that look like this:

53,55,56,48,46,48,48

Not sure how Go expects me to handle this.

Solution.... stupid simple and not terribly obvious:

The solution: 

crazyBytes := []uint8("5760.00,5750.50")
    aString := string(crazyBytes)
strSlice := strings.Split(aString,",") // string representation of our array (of floats)
var floatz []float64
for _, x := range strSlice {
    fmt.Printf("At last, Float: %s 
",x)
    f,err := strconv.ParseFloat(x,64)
    if err != nil { fmt.Printf("Error: %s",err) }
    floatz = append(floatz, f)
    fmt.Printf("as float: %s 
", strconv.FormatFloat(f,'f',-1,64))
}

Yea sure, it's obvious NOW.

  • 写回答

1条回答 默认 最新

  • dony113407 2019-04-03 17:48
    关注

    GROUP_CONCAT returns a string. So in Go you get a byte array of characters, not a float. The result you posted 53,55,56,48,46,48,48 translates into a string "5780.00" which does look like one of your values. So you need to either fix your SQL to return floats or use strings and strconv modules in Go to parse and convert your string into floats. I think the former approach is better, but it is up to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动