go - golang - Elem Vs Indirect in the reflect package -
go - golang - Elem Vs Indirect in the reflect package -
from docs
func (v value) elem() value
elem returns value interface v contains or pointer v points to. panics if v's kind not interface or ptr. returns 0 value if v nil.
func indirect(v value) value
indirect returns value v points to. if v nil pointer, indirect returns 0 value. if v not pointer, indirect returns v.
so can safely assume following?
reflect.indirect(reflect.valueof(somex)) === reflect.valueof(somex).elem().
is indirect method convenience method right hand side of above?
if reflect.value
pointer, v.elem()
equivalent reflect.indirect(v)
. if not pointer, not equivalent:
reflect.indirect(v)
homecoming same value, while v.elem()
homecoming contained dynamic value. if value else, v.elem()
panic. the reflect.indirect
helper intended cases want take either particular type, or pointer type. 1 illustration database/sql
conversion routines: using reflect.indirect
, can utilize same code paths handle various types , pointers types.
go
Comments
Post a Comment