dsk61780 2018-06-16 21:41
浏览 146
已采纳

为什么IsValid()对于int的零值返回true?

Consider the following Go program:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    v := reflect.ValueOf(int(0))
    fmt.Printf("IsValid()? %v
", v.IsValid())
}

Given that the documentation for Value.IsValid states:

IsValid reports whether v represents a value. It returns false if v is the zero Value.

...and given that the zero value for int is 0, I would expect the program to report that IsValid() returned false. Unfortunately, this is not the case:

IsValid()? true

Why is this?

  • 写回答

1条回答 默认 最新

  • dounao1875 2018-06-16 23:57
    关注

    Package reflect

    import "reflect" 
    

    type Value

    Value is the reflection interface to a Go value.

    The zero Value represents no value. Its IsValid method returns false, its Kind method returns Invalid, its String method returns "", and all other methods panic.

    func ValueOf

    func ValueOf(i interface{}) Value
    

    ValueOf returns a new Value initialized to the concrete value stored in the interface i. ValueOf(nil) returns the zero Value.

    func (Value) IsValid

    func (v Value) IsValid() bool
    

    IsValid reports whether v represents a value. It returns false if v is the zero Value. If IsValid returns false, all other methods except String panic. Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.


    int(0) is a concrete value. ValueOf returns a new Value initialized to the concrete value stored in the interface i. ValueOf(nil) returns the zero Value. IsValid reports whether v represents a concrete value. It returns false if v is the zero Value.

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    func main() {
        fmt.Printf("IsValid(nil) %v
    ", reflect.ValueOf(nil).IsValid())
        fmt.Printf("IsValid(int(0)) %v
    ", reflect.ValueOf(int(0)).IsValid())
    }
    

    Output:

    IsValid(nil) false
    IsValid(int(0)) true
    

    The Go Programming Language Specification

    The zero value

    When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value is set to the zero value for its type: false for booleans, 0 for numeric types, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.


    Package reflect

    import "reflect" 
    

    func Zero

    func Zero(typ Type) Value
    

    Zero returns a Value representing the zero value for the specified type. The result is different from the zero value of the Value struct, which represents no value at all. For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0. The returned value is neither addressable nor settable.


    The Go programming language zero value is not the same as the zero Value in the reflect package. Note the difference in the capitalization of the words Value and value. For example, reflect.Zero returns a Value representing the zero value for the specified type. The result is different from the zero value of the Value struct, which represents no value at all.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能