Nathan Osman
Nathan Osman

Reputation: 73275

Why is IsValid() returning true for the zero value of an int?

Consider the following Go program:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    v := reflect.ValueOf(int(0))
    fmt.Printf("IsValid()? %v\n", 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?

Upvotes: 3

Views: 680

Answers (1)

peterSO
peterSO

Reputation: 166744

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\n", reflect.ValueOf(nil).IsValid())
    fmt.Printf("IsValid(int(0)) %v\n", 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.

Upvotes: 3

Related Questions