gpt4 book ai didi

go - 类型开关不检测 float32

转载 作者:数据小太阳 更新时间:2023-10-29 03:46:52 24 4
gpt4 key购买 nike

package main

import "fmt"

func main() {
printType(4.0)
}

func printType(i interface{}) {
switch i := i.(type) {
case float32:
fmt.Println("This is a float type", i)
}
}

出于某种原因,此代码未将 4.0 值检测为 float32,但将其检测为 float64 - 这是为什么呢?我在 win x64 机器上运行它。

最佳答案

For some reason this code does not detect 4.0 value as float32 but it detects it as float64 - why is that?

因为 float64default type对于无类型浮点常量。相关部分强调:

An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.

关于go - 类型开关不检测 float32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045074/

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