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 如何修改别人网站内的数字,各位接单的私我
  • ¥15 poi怎么在指定位置之后生成多个表格
  • ¥15 nginx在location设置了client_max_body_size依旧报错413
  • ¥15 C#添加WCF服务引用客户端调用方法没有反应
  • ¥15 stm32f103c8t6与esp8266
  • ¥15 使用Hadoop的MapReduce program
  • ¥15 Python发生IndexError错误如何解决?
  • ¥15 如何用matlab搭建激光器
  • ¥15 51单片机两路互补SPWM输出全桥逆变
  • ¥15 outlook左边用户名下拉为啥打不开了