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.

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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?