Parsing strconv.ParseFloat在不应';T

Parsing strconv.ParseFloat在不应';T,parsing,go,floating-point,gccgo,Parsing,Go,Floating Point,Gccgo,我有以下代码: buffer := make([]byte, 256) conn.Read(buffer) result:= string(buffer) fmt.Println("Val:", result) floatResult, _ := strconv.ParseFloat(result, 64) fmt.Println("Val on float:", floatResult) 输出如下: Val: 40.385167 Val on fl

我有以下代码:

buffer := make([]byte, 256)
conn.Read(buffer)

result:= string(buffer)
fmt.Println("Val:", result)

floatResult, _ := strconv.ParseFloat(result, 64)
fmt.Println("Val on float:", floatResult)
输出如下:

Val: 40.385167
Val on float: 0

这似乎是将字符串解析为
float64
的正确方法,我缺少什么吗?

您可以使用
len
等于读取字节来创建新切片(查看)

buffer:=make([]字节,256)

i、 @wim-oh-yes:strconv.ParseFloat:parsing返回错误吗?@wim-oh-yes:strconv.ParseFloat:parsing"-0.000000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0000\0\0 0 0\0 0 0\0 0 0 0\\0 0 0 0\0 0 0\0 0 0\0 0 0\0 0 0\\0 0 0\0 0 0\0 0 0\0 0 0\0 0 0\0 0 0\0 0 0\0 0 0\0 0\\\0 0 0\\0 0\\0 0\\\\0 0 0 0 0\\0 0 0 0 0\\\\0 0 0 0 0 0 0 0\\\\\0 0 0 0 0 0 0 0 0 0 0 0 0\\\\\\\\\\\0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\\\\\\\\\\\\\\\\\\\\\0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\\\\\\\\\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00。。。。。。。。。。无效语法通过更改字节数组大小以匹配我的结果来解决此问题,如果我的字节数组大小超过此值,则会附加大量“\x00…”是的,您应该始终检查Go中的错误;有时,如果您只想快速测试某些内容,可以跳过它,但如果它不起作用,则首先要检查:-)在“真实”生产代码中,您应该始终检查错误。1)应用程序忽略读取的字节数和连接中的错误。因为不能保证读取会填满缓冲区(这里也不保证),所以应该始终使用字节计数。2) 假设这是一个TCP连接,不能保证该号码中的所有字节都是通过一次调用读取的。如果发送多个值,应用程序需要实现某种消息框架;如果发送单个值,应用程序需要将其读取到EOF。