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.

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

报告相同问题?

悬赏问题

  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角