gpt4 book ai didi

go - ParseInt 不转换为所需的类型

转载 作者:IT王子 更新时间:2023-10-29 02:26:07 25 4
gpt4 key购买 nike

这是我的代码:

主要包

import (
"fmt"
"reflect"
"strconv"
)

func main() {
i, _ := strconv.ParseInt("10", 10, 8)
fmt.Println(reflect.TypeOf(i))
}

我希望 i 的长度为 8 位(strconv.ParseInt 的第三个参数)。然而,它是 int64(并且文档声明 strconv.ParseInt 将返回 int64)。

如果 ParseInt 总是返回 int64 有什么意义(为什么不直接使用 Atoi?)

最佳答案

请注意函数文档中的这一点:

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. For a bitSize below 0 or above 64 an error is returned.

因此可以保证您可以使用 byte(i) 将结果转换为字节。

Go 还没有泛型,因此很难有一个可以接受指向多个整数类型的指针的 ParseInt。相反,保证是通过 bitSize 参数

完成的

关于go - ParseInt 不转换为所需的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55925894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com