doujiao3074 2016-07-21 18:13
浏览 396
已采纳

检查golang中的空浮点数或整数值

I'm trying to check if my integer or float value is empty. But type error is thrown.

Tried:

if foo == nil    
//Error: cannot convert nil to type float32
//all other methods I Tried also throw type errors too
  • 写回答

1条回答 默认 最新

  • dongqijuan3786 2016-07-21 18:57
    关注

    The zero values for integer and floats is 0. nil is not a valid integer or float value.

    A pointer to an integer or a float can be nil, but not its value.

    This means you either check for the zero value:

    if foo == 0 {
      // it's a zero value
    }
    

    Or you deal with pointers:

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        var intPointer *int
    
        // To set the value use:
        // intValue := 3
        // intPointer = &intValue
    
        if intPointer == nil {
            fmt.Println("The variable is nil")
        } else {
            fmt.Printf("The variable is set to %v
    ", *intPointer)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机