package main
import (
"fmt"
"github.com/spf13/cast"
)
func main() {
v := cast.ToInt64("08")
fmt.Println(" v : ", v)
v = cast.ToInt64("09")
fmt.Println(" v1 : ", v)
}
ret: v : 0 v1 : 0
小时转换成整形的时候发现08 和09 获取的结果出错,经过验证和issue中查询发现 如果是0 开头的会考虑进制的问题;因此转化出错; lib对应issue在这里有人提出过: cast.ToInt(“08”) returned 0
得到的回复:
yveshield commented on Jul 7, 2022 The string “08” is not a decimal number, decimal numbers do not have the prefix “0”, nor is it an octal number, octal numbers are only 0-7, nor is it a hexadecimal number, the hexadecimal prefix is “0x “. I don’t think this is a problem with the library, I think it’s important to be aware of this limitation before using the library and avoid it in advance.
The string “08” is not a decimal number, decimal numbers do not have the prefix “0”, nor is it an octal number, octal numbers are only 0-7, nor is it a hexadecimal number, the hexadecimal prefix is “0x “. I don’t think this is a problem with the library, I think it’s important to be aware of this limitation before using the library and avoid it in advance. 字符串“08”不是十进制数,十进制数没有前缀“0”,也不是八进制数,八进制数只有0-7,也不是十六进制数,十六进制前缀是“0x”。我不认为这是lib的问题,我认为在使用lib之前意识到这一限制并提前避免它很重要。
a := []string{“01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, “09”} for _, v := range a { fmt.Println(cast.ToInt64(v)) } // output 1 2 3 4 5 6 7 0 0 // bug 08 => 0 09 =>0
@gonejack gonejack commented on Sep 20, 2022 This is not a bug — since these leading 0s is treated as octal, or base-8 number system. 这不是一个错误,因为这些前导0被视为八进制或以8为基数的数字系统。
本文链接:https://iokde.com/post/golang-cast64-snare.html,参与评论 »
--EOF--
发表于 2023-03-10 17:03:38。
本站使用「署名 4.0 国际」创作共享协议,转载请注明作者及原网址。tools更多说明 »
提醒:本文最后更新于 452 天前,文中所描述的信息可能已发生改变,请谨慎使用。
Comments