dszsajhd237437 2019-01-04 19:42
浏览 19
已采纳

类型开关未检测到float32

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)
    }
}

For some reason this code does not detect 4.0 value as float32 but it detects it as float64 - why is that? I run it on win x64 machine.

  • 写回答

1条回答 默认 最新

  • dongxiao_0528 2019-01-04 19:45
    关注

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

    Because float64 is the default type for an untyped floating-point constant. Relevant part emphasized:

    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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度