Reflection go

reflect.Valueof in the end is what?

 

 

 

Reflecting the value of the object (reflect.Value) provides a series of methods for determining a value of zero and null, as shown in the following table.

Value of zero reflectance value and a validity judgment object methods
Methods Description
IsNil() bool The return value is nil. If the value is not the type of channel (Channel), functions, interfaces, panic occurs map, a pointer or a slice, similar language layer v== niloperation
IsValid() bool Determining whether a valid value. When the value itself illegal, it returns false, for example, reflect Value does not contain any values, and so is nil.


The following example will be a null pointer is performed in various ways IsNil () and the IsValid () Returns the value determination is detected. While the map to find the structure of the key member and the return value of the method is the IsValid () is determined, with reference to the following code.

 

A Code:
zero value and object reflectance value validity judgment:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. // * int null pointer
  8. var a int *
  9. fmt.Println("var a *int:", reflect.ValueOf(a).IsNil())
  10. // nil value
  11. fmt.Println("nil:", reflect.ValueOf(nil).IsValid())
  12. // * int type of null pointer
  13. fmt.Println("(*int)(nil):", reflect.ValueOf((*int)(nil)).Elem().IsValid())
  14. // instantiate a structure
  15. s := struct{}{}
  16. // try to find the structure in a field that does not exist
  17. fmt.Println ( "structure member does not exist:", reflect.ValueOf (s) .FieldByName ( "") IsValid ().)
  18. // try to find the structure in a method that does not exist
  19. fmt.Println ( "method of the structure does not exist:", reflect.ValueOf (s) .MethodByName ( "") IsValid ().)
  20. // instantiate a map
  21. m := map[int]int{}
  22. // try to find the map from a non-existent key
  23. fmt.Println ( "key does not exist:"., reflect.ValueOf (m) .MapIndex (reflect.ValueOf (3)) IsValid ())
  24. }

Code output below:

A int * var: to true
nil: false
(int *) (nil): false
structure member does not exist: false
structure does not exist method: false
absence bond: false

Code as follows:

  • Line 11, a declared type * int pointer, the initial value is nil.
  • Line 12, packaged as a variable and determines whether reflect.Value is empty, then the variable a is a null pointer, thus returns true.
  • Line 15, to nil for the IsValid () determination (a validity determination), returns false.
  • Line 18, (* int) (nil) nil meaning is converted to * int, i.e. null pointer type * int. This line is converted to type * int nil, and takes a pointer to an element. Since the nil points to no element, * int type nil not point to any element value is not valid. Thus using this reflectance value isValid () returns false is determined.
  • Line 21, examples of a structure.
  • Line 24, s Find Members structural body by an empty string FieldByName, such as a member does not exist, the IsValid () returns false.
  • Line 27, to find a method by MethodByName s structure of an empty string, such as the method does not exist, the IsValid () returns false.
  • Line 30, examples of a map, map equivalent to such an approach and make way created.
  • Line 33, MapIndex () method can find the map based on the value given reflect.Value type, and returns the found result.


IsNil () is often used to determine whether the pointer is NULL; the IsValid () is often used to determine whether a valid return value.

 

 

 

 

 

 

 

 

 

 

Code II:

 

    a := map[string]int{}
    a["aa"] = 2
    b := reflect.ValueOf(a).MapIndex(reflect.ValueOf("aa"))
    c := b.Kind()
    fmt.Println (c == reflect.Int) // prints true

Guess you like

Origin www.cnblogs.com/saolv/p/11980561.html
Recommended