In golang reflect package, reflect.value.IsValid has those comments:
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.
Read above, i am very confused. i don't know what is zero value of reflect.Value and use it call isValid get false.
I write some code to test:
var haha *int
fmt.Println(reflect.ValueOf(haha).IsValid()) //true
var hehe interface{}
fmt.Println(reflect.ValueOf(hehe).IsValid() //false
A zero value is the default initialization value that is written to the memory address when no explicit initialization is provided. Tesing if v is a zero value is, in fact, testing if you have initialized v.
var haha *int
returns true because haha is a pointer and cannot be created with an rational "default zero value", otherwise it would point to a random address in your memory and could have dangerous side effects. Pointers are nil when they are created, when other data types will have an actual value.
On the other hand, the zero value of var hehe interface{}
is a nil *int, which is an actual entity.