Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does reflect.IsValid return false?

Tags:

reflection

go

I am curious about IsValid function, because during my use of this function, it never returned false. So when does it return a negative result?

like image 683
yskyj Avatar asked Aug 18 '16 06:08

yskyj


People also ask

How do you know if reflection is nil?

The reflect. IsNil() Function in Golang is used to check whether its argument v is nil. The argument must be a chan, func, interface, map, pointer, or slice value; if it is not, IsNil panics.

What is reflect value?

The reflect. ValueOf() Function in Golang is used to get the new Value initialized to the concrete value stored in the interface i. To access this function, one needs to imports the reflect package in the program.

What is reflect indirect?

The reflect. Indirect() Function in Golang is used to get the value that v points to, i.e., If v is a nil pointer, Indirect returns a zero Value. If v is not a pointer, Indirect returns v. To access this function, one needs to imports the reflect package in the program.

What is reflect in go?

Reflection is the ability of a program to introspect and analyze its structure during run-time. In Go language, reflection is primarily carried out with types. The reflect package offers all the required APIs/Methods for this purpose. Reflection is often termed as a method of metaprogramming.


1 Answers

As the doc reflect.IsValid() says:

It returns false if v is the zero Value. [...] Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.

Value.IsValid() is supposed to report whether the reflect.Value itself is valid, not the value it wraps (if any).

All the examples below print false. You can try them on the Go Playground.

The simplest example is calling IsValid() on the zero value of reflect.Value (which is a struct):

fmt.Println(reflect.Value{}.IsValid())

The 2nd simplest example is when passing nil to reflect.ValueOf():

fmt.Println(reflect.ValueOf(nil).IsValid())

Another example: start with a pointer being nil, in this case there is no "pointed" value, a nil pointer points to nowhere. Attempting to get the reflect.Value of the pointed value using Value.Elem() results in a zero reflect.Value whose IsValid() method will return false:

var i *int
v := reflect.ValueOf(i)
v2 := v.Elem()
fmt.Println(v2.IsValid())

Or in one line:

fmt.Println(reflect.ValueOf((*int)(nil)).Elem().IsValid())

Same thing if you call Value.Indirect() on the above reflect.Value():

fmt.Println(reflect.Indirect(v).IsValid())

Or attempting to get a non-existing struct field by name using Value.FieldByName():

s := struct{}{}
fmt.Println(reflect.ValueOf(s).FieldByName("").IsValid())

Or attempting to get a non-existing method by name using Value.MethodByName():

fmt.Println(reflect.ValueOf(s).MethodByName("").IsValid())

Or attempting to get a value from a map by a non-existing key using Value.MapIndex():

m := map[int]int{}
fmt.Println(reflect.ValueOf(m).MapIndex(reflect.ValueOf(3)).IsValid())

The list goes on...

like image 183
icza Avatar answered Sep 29 '22 09:09

icza